{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1、导入 numpy 作为 np，并查看版本\n",
    "\n",
    "**难度等级：** L1\n",
    "\n",
    "**问题：** 将 numpy 导入为 np 并打印版本号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1.15.4'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "np.__version__"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2、如何创建一维数组？\n",
    "\n",
    "**难度等级：** L1\n",
    "\n",
    "**问题：** 创建从 0 到 9 的一维数字数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.arange(10)\n",
    "arr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3、如何创建一个布尔数组？\n",
    "\n",
    "**难度等级：** L1\n",
    "\n",
    "**问题：** 创建一个 numpy 数组元素值全为 True（真）的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ True  True  True]\n",
      " [ True  True  True]\n",
      " [ True  True  True]]\n",
      "[[ True  True  True]\n",
      " [ True  True  True]\n",
      " [ True  True  True]]\n"
     ]
    }
   ],
   "source": [
    "print(np.full((3, 3), True, dtype = bool))\n",
    "print(np.ones((3, 3), dtype = bool))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4、如何从一维数组中提取满足指定条件的元素？\n",
    "\n",
    "**难度等级：** L1\n",
    "\n",
    "**问题：** 从 arr 中提取所有的奇数\n",
    "\n",
    "**给定：**\n",
    "> arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])\n",
    "\n",
    "**期望的输出：**\n",
    "> array([1, 3, 5, 7, 9])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 3, 5, 7, 9])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])\n",
    "odd_arr = arr[arr % 2 != 0]\n",
    "odd_arr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5、如何用 numpy 数组中的另一个值替换满足条件的元素项？\n",
    "\n",
    "**难度等级：** L1\n",
    "\n",
    "**问题：** 将 arr 中的所有奇数替换为 -1\n",
    "\n",
    "**给定：**\n",
    "> arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])\n",
    "\n",
    "**期望的输出：**\n",
    "> array([0, -1, 2, -1, 4, -1, 6, -1, 8, -1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0, -1,  2, -1,  4, -1,  6, -1,  8, -1])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])\n",
    "arr[arr % 2 != 0] = -1\n",
    "arr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6、如何在不影响原始数组的情况下替换满足条件的元素项？\n",
    "\n",
    "**难度等级：** L2\n",
    "\n",
    "**问题：** 将 arr 中的所有奇数替换为 -1，而不改变 arr\n",
    "\n",
    "**给定：**\n",
    "> arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])\n",
    "\n",
    "**期望的输出：**\n",
    "> out >\n",
    "> array([0, -1, 2, -1, 4, -1, 6, -1, 8, -1])\n",
    "\n",
    "> arr >\n",
    "> array([0, 1, 2, 3, 4, 5, 6, 7, 8, ])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0 -1  2 -1  4 -1  6 -1  8 -1]\n",
      "[0 1 2 3 4 5 6 7 8 9]\n"
     ]
    }
   ],
   "source": [
    "arr = np.arange(10)\n",
    "out = np.where(arr % 2 == 1, -1, arr)\n",
    "print(out)\n",
    "print(arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 7、如何改变数组的形状？\n",
    "\n",
    "**难度等级：** L1\n",
    "\n",
    "**问题：** 将一维数组转换为 2 行的 2 维数组\n",
    "\n",
    "**给定：**\n",
    "> np.arange(10)\n",
    "\n",
    "**期望的输出：**\n",
    ">array([[0, 1, 2, 3, 4],  \n",
    "&emsp;&emsp;[5, 6, 7, 8, 9]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4],\n",
       "       [5, 6, 7, 8, 9]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.arange(10)\n",
    "arr = arr.reshape(2, -1)\n",
    "arr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 8、如何垂直叠加两个数组？\n",
    "\n",
    "**难度等级：** L2\n",
    "\n",
    "**问题：** 垂直堆叠数组 a 和数组 b\n",
    "\n",
    "**给定：**\n",
    "> a = np.arange(10).reshape(2, -1)  \n",
    "b = np.repeat(1, 10).reshape(2, -1)\n",
    "\n",
    "**期望的输出：**\n",
    "> array([[0, 1, 2, 3, 4],  \n",
    "&emsp;&emsp;[5, 6, 7, 8, 9],  \n",
    "&emsp;&emsp;[1, 1, 1, 1, 1],  \n",
    "&emsp;&emsp;[1, 1, 1, 1, 1]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4],\n",
       "       [5, 6, 7, 8, 9],\n",
       "       [1, 1, 1, 1, 1],\n",
       "       [1, 1, 1, 1, 1]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(10).reshape(2, -1)\n",
    "b = np.repeat(1, 10).reshape(2, -1)\n",
    "\n",
    "# 法一\n",
    "np.concatenate([a, b], axis = 0)\n",
    "\n",
    "# 法二 stack 增加维度，hstack 水平叠加，vstack 垂直叠加\n",
    "np.vstack([a, b])\n",
    "\n",
    "# 法三\n",
    "np.r_[a, b]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 9、如何水平叠加两个数组？\n",
    "\n",
    "**难度等级：** L2\n",
    "\n",
    "**问题：**将数组 a 和数组 b 水平堆叠\n",
    "\n",
    "**给定：**\n",
    "> a = np.arange(10).reshape(2, -1)  \n",
    "b = np.repeat(1, 10).reshape(2, -1)\n",
    "\n",
    "**期望的输出：**\n",
    "> array([[0, 1, 2, 3, 4, 1, 1, 1, 1, 1]]  \n",
    "&emsp;&emsp;[5, 6, 7, 8, 9，1, 1, 1, 1, 1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4, 1, 1, 1, 1, 1],\n",
       "       [5, 6, 7, 8, 9, 1, 1, 1, 1, 1]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(10).reshape(2, -1)\n",
    "b = np.repeat(1, 10).reshape(2, -1)\n",
    "\n",
    "# 法一\n",
    "np.concatenate([a, b], axis = 1))\n",
    "\n",
    "# 法二\n",
    "np.hstack([a, b])\n",
    "\n",
    "# 法三\n",
    "np.c_[a, b]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 10、如何在无硬编码的情况下生成 numpy 中的自定义序列？\n",
    "\n",
    "**难度等级：** L2\n",
    "\n",
    "**问题：** 创建以下模式而不是用硬编码。只使用 numpy 函数和下面的输入数组 a。\n",
    "\n",
    "**给定：**\n",
    "> a= np.array([1, 2, 3])\n",
    "\n",
    "**期望的输出：**\n",
    "> array([1, 1, 1, 2, 2, 2, 3, 3, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 1, 1, 2, 2, 2, 3, 3, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a= np.array([1, 2, 3])\n",
    "np.r_[np.repeat(a, 3), np.tile(a, 3)]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 11、如何获取两个 numpy 数组之间的公共项？\n",
    "\n",
    "**难度等级：** L2\n",
    "\n",
    "**问题：** 获取数组 a 和数组 b 之间的公共项\n",
    "\n",
    "**给定：**\n",
    "> a = np.array([1, 2, 3, 2, 3, 4, 3, 4, 5, 6])  \n",
    "b = np.array([7, 2, 10, 2, 7, 4, 9, 4, 9, 8])\n",
    "\n",
    "**期望输出：**\n",
    "> array([2, 4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 4])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.array([1, 2, 3, 2, 3, 4, 3, 4, 5, 6])\n",
    "b = np.array([7, 2, 10, 2, 7, 4, 9, 4, 9, 8])\n",
    "np.intersect1d(a, b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 12、如何从一个数组中删除存在于另一个数组中的项？\n",
    "\n",
    "**难度等级：** L2\n",
    "\n",
    "**问题：** 从数组 a 中删除数组 b 中的所有项\n",
    "\n",
    "**给定：**\n",
    "> a = np.array([1, 2, 3, 4, 5])  \n",
    "b = np.array([5, 6, 7, 8, 9])\n",
    "\n",
    "**期待的输出：**\n",
    "> array([1, 2, 3, 4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([1, 2, 3, 4, 5])\n",
    "b = np.array([5, 6, 7, 8, 9])\n",
    "\n",
    "np.setdiff1d(a, b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 13、如何得到两个数组元素匹配的位置？\n",
    "\n",
    "**难度等级：** L2\n",
    "\n",
    "**问题：** 获取 a 和 b 元素匹配的位置\n",
    "\n",
    "**给定：**\n",
    "> a = np.array([1, 2, 3, 2, 3, 4, 3, 4, 5, 6])  \n",
    "b = np.array([7, 2, 10, 2, 7, 4, 9, 4, 9, 8])\n",
    "\n",
    "**期望的输出：**\n",
    "> array([1, 3, 5, 7])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([1, 3, 5, 7], dtype=int64),)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([1, 2, 3, 2, 3, 4, 3, 4, 5, 6])  \n",
    "b = np.array([7, 2, 10, 2, 7, 4, 9, 4, 9, 8])\n",
    "\n",
    "np.where(a == b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 14、如何从 numpy 数组中提取给定范围内的所有数字？\n",
    "\n",
    "**难度等级：** L2\n",
    "\n",
    "**问题：** 获取 5 到 10 之间的所有项目\n",
    "\n",
    "**给定：**\n",
    "> a = np.array([2, 6, 1, 9, 10, 3, 27])  \n",
    "\n",
    "**期望的输出：**\n",
    "> array([6, 9, 10])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 6  9 10]\n",
      "[ 6  9 10]\n",
      "[ 6  9 10]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([2, 6, 1, 9, 10, 3, 27])  \n",
    "\n",
    "# 法一\n",
    "index = np.where((a >= 5) & (a <= 10))\n",
    "print(a[index])\n",
    "\n",
    "# 法二\n",
    "index = np.where(np.logical_and(a >= 5, a <= 10))\n",
    "print(a[index])\n",
    "\n",
    "# 法三\n",
    "print(a[(a >= 5 ) & ( a <= 10 )])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 15、如何创建一个 python 函数来处理 scalars 并在 numpy 数组上工作？\n",
    "\n",
    "**难度等级：** L2\n",
    "\n",
    "**问题：** 转换适用于两个标量的函数 maxx，以处理两个数组\n",
    "\n",
    "**给定：**\n",
    "``` python\n",
    "def maxx(x, y):\n",
    "    \"\"\" get the maximum of two items\"\"\"\n",
    "    if x >= y:\n",
    "        return x\n",
    "    else:\n",
    "        return y\n",
    "    \n",
    "maxx(1, 5)\n",
    "```\n",
    "\n",
    "**期望的输出：**\n",
    "``` python\n",
    "a = np.array([5, 7, 9, 8, 6, 4, 5])\n",
    "b = np.array([6, 3, 4, 8, 9, 7, 1])\n",
    "\n",
    "pair_max(a, b)\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([6., 7., 9., 8., 9., 7., 5.])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def maxx(x, y):\n",
    "    \"\"\" get the maximum of two items\"\"\"\n",
    "    if x >= y:\n",
    "        return x\n",
    "    else:\n",
    "        return y\n",
    "\n",
    "maxx(1, 5)\n",
    "\n",
    "pair_max = np.vectorize(maxx, otypes = [float])\n",
    "\n",
    "a = np.array([5, 7, 9, 8, 6, 4, 5])\n",
    "b = np.array([6, 3, 4, 8, 9, 7, 1])\n",
    "\n",
    "pair_max(a, b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 16、如何交换二维 numpy 数组中的两列？\n",
    "\n",
    "**难度等级：** L2\n",
    "\n",
    "**问题：** 在数组 arr 中交换列 1 和 2\n",
    "\n",
    "**给定：**\n",
    "``` Python\n",
    "arr = np.arange(9).reshape(3, 3)\n",
    "arr\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 0, 2],\n",
       "       [4, 3, 5],\n",
       "       [7, 6, 8]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.arange(9).reshape(3, 3)\n",
    "arr[:, [1, 0, 2]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 17、如何交换二维 numpy 数组中的两行？\n",
    "\n",
    "**难度等级：** L2\n",
    "\n",
    "**问题：** 交换数组 arr 中的第 1 和第 2 行\n",
    "\n",
    "**给定：**\n",
    "``` Python\n",
    "arr = np.arange(9).reshape(3, 3)\n",
    "arr\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 4, 5],\n",
       "       [0, 1, 2],\n",
       "       [6, 7, 8]])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.arange(9).reshape(3, 3)\n",
    "arr[[1, 0, 2], :]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 18、如何翻转二维数组的行？\n",
    "\n",
    "**难度等级：** L2\n",
    "\n",
    "**问题：** 反转二维数组 arr 的行\n",
    "\n",
    "**给定：**\n",
    "``` python\n",
    "arr = np.arange(9).reshape(3, 3)\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 7, 8],\n",
       "       [3, 4, 5],\n",
       "       [0, 1, 2]])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.arange(9).reshape(3, 3)\n",
    "\n",
    "arr[::-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 19、如何翻转二维数组的列？\n",
    "\n",
    "**难度等级：** L2\n",
    "\n",
    "**问题：** 反转二维数组 arr 的列\n",
    "\n",
    "**给定：**\n",
    "``` python\n",
    "arr = np.arange(9).reshape(3, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 1, 0],\n",
       "       [5, 4, 3],\n",
       "       [8, 7, 6]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.arange(9).reshape(3, 3)\n",
    "\n",
    "arr[:, ::-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 20、如何创建包含 5 到 10 之间随机浮动的二维数组？\n",
    "\n",
    "**难度等级：** L2\n",
    "\n",
    "**问题：** 创建一个形状为 5 * 3 的二维数组，以包含 5 到 10 之间的随机十进制数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[7.27319935 8.82957811 8.20110394]\n",
      " [9.18435796 6.66728611 5.29464103]\n",
      " [6.58244715 7.52513584 5.90215226]\n",
      " [5.26767929 7.45774061 6.47781293]\n",
      " [8.14353907 6.74745956 6.09625331]]\n"
     ]
    }
   ],
   "source": [
    "rand_arr = np.random.randint(low = 5, high = 10, size = (5, 3)) + np.random.random((5, 3))\n",
    "\n",
    "rand_arr = np.random.uniform(5, 10, size = (5, 3))\n",
    "print(rand_arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 21、如何在 numpy 数组中打印小数点后三位？\n",
    "\n",
    "**难度等级：** L1\n",
    "\n",
    "**问题：** 只打印或显示 numpy 数组 rand_arr 的小数点后 3 位\n",
    "\n",
    "**给定：**\n",
    "``` python\n",
    "rand_arr = np.random.random((5, 3))\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.574, 1.   , 0.726],\n",
       "       [0.559, 0.713, 0.817],\n",
       "       [0.581, 0.334, 0.97 ],\n",
       "       [0.3  , 0.227, 0.588]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "rand_arr = np.random.random((5, 3))\n",
    "\n",
    "np.set_printoptions(precision = 3)\n",
    "rand_arr[:4]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 22、如何通过 e 式科学计数法（如 1E+10）来打印一个 numpy 数组？\n",
    "\n",
    "**难度等级：** L1\n",
    "\n",
    "**问题：** 通过 e 式科学计数法来打印 rand_arr（如 1e10）\n",
    "\n",
    "**给定：**\n",
    "``` Python\n",
    "np.random.seed(100)\n",
    "rand_arr = np.random.random([3, 3]) / 1e3\n",
    "rand_arr\n",
    "```\n",
    "\n",
    "**期望的输出：**\n",
    "> array([[ 0.000543,  0.000278,  0.000425],  \n",
    "&emsp;&emsp;[ 0.000845,  0.000005,  0.000122],  \n",
    "&emsp;&emsp;[ 0.000671,  0.000826,  0.000137]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.000543, 0.000278, 0.000425],\n",
       "       [0.000845, 0.000005, 0.000122],\n",
       "       [0.000671, 0.000826, 0.000137]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.set_printoptions(suppress = False)\n",
    "\n",
    "np.random.seed(100)\n",
    "rand_arr = np.random.random([3, 3]) / 1e3\n",
    "rand_arr\n",
    "\n",
    "np.set_printoptions(suppress = True, precision = 6)\n",
    "rand_arr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 23、如何限制 numpy 数组输出中打印的项目数？\n",
    "\n",
    "**难度等级：** L1\n",
    "\n",
    "**问题：** 将 numpy 数组 a 中打印的项数限制为最多 6 个元素\n",
    "\n",
    "**给定：**\n",
    "``` python\n",
    "a = np.arange(15)\n",
    "```\n",
    "\n",
    "**期望的输出：**\n",
    "> array([1, 2, 3, ..., 12, 13, 14])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2, ..., 12, 13, 14])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.set_printoptions(threshold = 6)\n",
    "a = np.arange(15)\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 24、如何打印完整的 numpy 数组而不截断\n",
    "\n",
    "**难度等级：** L1\n",
    "\n",
    "**问题：** 打印完整的 numpy 数组 a 而不截断\n",
    "\n",
    "**给定：**\n",
    "``` python\n",
    "np.set_printoptions(threshold = 6)\n",
    "a = np.arange(15)\n",
    "a\n",
    "```\n",
    "\n",
    "**期望的输出：**\n",
    "> a  \n",
    "array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.set_printoptions(threshold = 6)\n",
    "a = np.arange(15)\n",
    "\n",
    "np.set_printoptions(threshold = np.nan)\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 25、如何导入数字和文本的数据集保持文本在 numpy 数组中完好无损？\n",
    "\n",
    "**难度等级：** L2\n",
    "\n",
    "**问题：** 导入鸢尾属植物数据集，保持文本不变"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[b'5.1', b'3.5', b'1.4', b'0.2', b'Iris-setosa'],\n",
       "       [b'4.9', b'3.0', b'1.4', b'0.2', b'Iris-setosa'],\n",
       "       [b'4.7', b'3.2', b'1.3', b'0.2', b'Iris-setosa'],\n",
       "       [b'4.6', b'3.1', b'1.5', b'0.2', b'Iris-setosa'],\n",
       "       [b'5.0', b'3.6', b'1.4', b'0.2', b'Iris-setosa'],\n",
       "       [b'5.4', b'3.9', b'1.7', b'0.4', b'Iris-setosa'],\n",
       "       [b'4.6', b'3.4', b'1.4', b'0.3', b'Iris-setosa'],\n",
       "       [b'5.0', b'3.4', b'1.5', b'0.2', b'Iris-setosa'],\n",
       "       [b'4.4', b'2.9', b'1.4', b'0.2', b'Iris-setosa'],\n",
       "       [b'4.9', b'3.1', b'1.5', b'0.1', b'Iris-setosa'],\n",
       "       [b'5.4', b'3.7', b'1.5', b'0.2', b'Iris-setosa'],\n",
       "       [b'4.8', b'3.4', b'1.6', b'0.2', b'Iris-setosa'],\n",
       "       [b'4.8', b'3.0', b'1.4', b'0.1', b'Iris-setosa'],\n",
       "       [b'4.3', b'3.0', b'1.1', b'0.1', b'Iris-setosa'],\n",
       "       [b'5.8', b'4.0', b'1.2', b'0.2', b'Iris-setosa'],\n",
       "       [b'5.7', b'4.4', b'1.5', b'0.4', b'Iris-setosa'],\n",
       "       [b'5.4', b'3.9', b'1.3', b'0.4', b'Iris-setosa'],\n",
       "       [b'5.1', b'3.5', b'1.4', b'0.3', b'Iris-setosa'],\n",
       "       [b'5.7', b'3.8', b'1.7', b'0.3', b'Iris-setosa'],\n",
       "       [b'5.1', b'3.8', b'1.5', b'0.3', b'Iris-setosa'],\n",
       "       [b'5.4', b'3.4', b'1.7', b'0.2', b'Iris-setosa'],\n",
       "       [b'5.1', b'3.7', b'1.5', b'0.4', b'Iris-setosa'],\n",
       "       [b'4.6', b'3.6', b'1.0', b'0.2', b'Iris-setosa'],\n",
       "       [b'5.1', b'3.3', b'1.7', b'0.5', b'Iris-setosa'],\n",
       "       [b'4.8', b'3.4', b'1.9', b'0.2', b'Iris-setosa'],\n",
       "       [b'5.0', b'3.0', b'1.6', b'0.2', b'Iris-setosa'],\n",
       "       [b'5.0', b'3.4', b'1.6', b'0.4', b'Iris-setosa'],\n",
       "       [b'5.2', b'3.5', b'1.5', b'0.2', b'Iris-setosa'],\n",
       "       [b'5.2', b'3.4', b'1.4', b'0.2', b'Iris-setosa'],\n",
       "       [b'4.7', b'3.2', b'1.6', b'0.2', b'Iris-setosa'],\n",
       "       [b'4.8', b'3.1', b'1.6', b'0.2', b'Iris-setosa'],\n",
       "       [b'5.4', b'3.4', b'1.5', b'0.4', b'Iris-setosa'],\n",
       "       [b'5.2', b'4.1', b'1.5', b'0.1', b'Iris-setosa'],\n",
       "       [b'5.5', b'4.2', b'1.4', b'0.2', b'Iris-setosa'],\n",
       "       [b'4.9', b'3.1', b'1.5', b'0.1', b'Iris-setosa'],\n",
       "       [b'5.0', b'3.2', b'1.2', b'0.2', b'Iris-setosa'],\n",
       "       [b'5.5', b'3.5', b'1.3', b'0.2', b'Iris-setosa'],\n",
       "       [b'4.9', b'3.1', b'1.5', b'0.1', b'Iris-setosa'],\n",
       "       [b'4.4', b'3.0', b'1.3', b'0.2', b'Iris-setosa'],\n",
       "       [b'5.1', b'3.4', b'1.5', b'0.2', b'Iris-setosa'],\n",
       "       [b'5.0', b'3.5', b'1.3', b'0.3', b'Iris-setosa'],\n",
       "       [b'4.5', b'2.3', b'1.3', b'0.3', b'Iris-setosa'],\n",
       "       [b'4.4', b'3.2', b'1.3', b'0.2', b'Iris-setosa'],\n",
       "       [b'5.0', b'3.5', b'1.6', b'0.6', b'Iris-setosa'],\n",
       "       [b'5.1', b'3.8', b'1.9', b'0.4', b'Iris-setosa'],\n",
       "       [b'4.8', b'3.0', b'1.4', b'0.3', b'Iris-setosa'],\n",
       "       [b'5.1', b'3.8', b'1.6', b'0.2', b'Iris-setosa'],\n",
       "       [b'4.6', b'3.2', b'1.4', b'0.2', b'Iris-setosa'],\n",
       "       [b'5.3', b'3.7', b'1.5', b'0.2', b'Iris-setosa'],\n",
       "       [b'5.0', b'3.3', b'1.4', b'0.2', b'Iris-setosa'],\n",
       "       [b'7.0', b'3.2', b'4.7', b'1.4', b'Iris-versicolor'],\n",
       "       [b'6.4', b'3.2', b'4.5', b'1.5', b'Iris-versicolor'],\n",
       "       [b'6.9', b'3.1', b'4.9', b'1.5', b'Iris-versicolor'],\n",
       "       [b'5.5', b'2.3', b'4.0', b'1.3', b'Iris-versicolor'],\n",
       "       [b'6.5', b'2.8', b'4.6', b'1.5', b'Iris-versicolor'],\n",
       "       [b'5.7', b'2.8', b'4.5', b'1.3', b'Iris-versicolor'],\n",
       "       [b'6.3', b'3.3', b'4.7', b'1.6', b'Iris-versicolor'],\n",
       "       [b'4.9', b'2.4', b'3.3', b'1.0', b'Iris-versicolor'],\n",
       "       [b'6.6', b'2.9', b'4.6', b'1.3', b'Iris-versicolor'],\n",
       "       [b'5.2', b'2.7', b'3.9', b'1.4', b'Iris-versicolor'],\n",
       "       [b'5.0', b'2.0', b'3.5', b'1.0', b'Iris-versicolor'],\n",
       "       [b'5.9', b'3.0', b'4.2', b'1.5', b'Iris-versicolor'],\n",
       "       [b'6.0', b'2.2', b'4.0', b'1.0', b'Iris-versicolor'],\n",
       "       [b'6.1', b'2.9', b'4.7', b'1.4', b'Iris-versicolor'],\n",
       "       [b'5.6', b'2.9', b'3.6', b'1.3', b'Iris-versicolor'],\n",
       "       [b'6.7', b'3.1', b'4.4', b'1.4', b'Iris-versicolor'],\n",
       "       [b'5.6', b'3.0', b'4.5', b'1.5', b'Iris-versicolor'],\n",
       "       [b'5.8', b'2.7', b'4.1', b'1.0', b'Iris-versicolor'],\n",
       "       [b'6.2', b'2.2', b'4.5', b'1.5', b'Iris-versicolor'],\n",
       "       [b'5.6', b'2.5', b'3.9', b'1.1', b'Iris-versicolor'],\n",
       "       [b'5.9', b'3.2', b'4.8', b'1.8', b'Iris-versicolor'],\n",
       "       [b'6.1', b'2.8', b'4.0', b'1.3', b'Iris-versicolor'],\n",
       "       [b'6.3', b'2.5', b'4.9', b'1.5', b'Iris-versicolor'],\n",
       "       [b'6.1', b'2.8', b'4.7', b'1.2', b'Iris-versicolor'],\n",
       "       [b'6.4', b'2.9', b'4.3', b'1.3', b'Iris-versicolor'],\n",
       "       [b'6.6', b'3.0', b'4.4', b'1.4', b'Iris-versicolor'],\n",
       "       [b'6.8', b'2.8', b'4.8', b'1.4', b'Iris-versicolor'],\n",
       "       [b'6.7', b'3.0', b'5.0', b'1.7', b'Iris-versicolor'],\n",
       "       [b'6.0', b'2.9', b'4.5', b'1.5', b'Iris-versicolor'],\n",
       "       [b'5.7', b'2.6', b'3.5', b'1.0', b'Iris-versicolor'],\n",
       "       [b'5.5', b'2.4', b'3.8', b'1.1', b'Iris-versicolor'],\n",
       "       [b'5.5', b'2.4', b'3.7', b'1.0', b'Iris-versicolor'],\n",
       "       [b'5.8', b'2.7', b'3.9', b'1.2', b'Iris-versicolor'],\n",
       "       [b'6.0', b'2.7', b'5.1', b'1.6', b'Iris-versicolor'],\n",
       "       [b'5.4', b'3.0', b'4.5', b'1.5', b'Iris-versicolor'],\n",
       "       [b'6.0', b'3.4', b'4.5', b'1.6', b'Iris-versicolor'],\n",
       "       [b'6.7', b'3.1', b'4.7', b'1.5', b'Iris-versicolor'],\n",
       "       [b'6.3', b'2.3', b'4.4', b'1.3', b'Iris-versicolor'],\n",
       "       [b'5.6', b'3.0', b'4.1', b'1.3', b'Iris-versicolor'],\n",
       "       [b'5.5', b'2.5', b'4.0', b'1.3', b'Iris-versicolor'],\n",
       "       [b'5.5', b'2.6', b'4.4', b'1.2', b'Iris-versicolor'],\n",
       "       [b'6.1', b'3.0', b'4.6', b'1.4', b'Iris-versicolor'],\n",
       "       [b'5.8', b'2.6', b'4.0', b'1.2', b'Iris-versicolor'],\n",
       "       [b'5.0', b'2.3', b'3.3', b'1.0', b'Iris-versicolor'],\n",
       "       [b'5.6', b'2.7', b'4.2', b'1.3', b'Iris-versicolor'],\n",
       "       [b'5.7', b'3.0', b'4.2', b'1.2', b'Iris-versicolor'],\n",
       "       [b'5.7', b'2.9', b'4.2', b'1.3', b'Iris-versicolor'],\n",
       "       [b'6.2', b'2.9', b'4.3', b'1.3', b'Iris-versicolor'],\n",
       "       [b'5.1', b'2.5', b'3.0', b'1.1', b'Iris-versicolor'],\n",
       "       [b'5.7', b'2.8', b'4.1', b'1.3', b'Iris-versicolor'],\n",
       "       [b'6.3', b'3.3', b'6.0', b'2.5', b'Iris-virginica'],\n",
       "       [b'5.8', b'2.7', b'5.1', b'1.9', b'Iris-virginica'],\n",
       "       [b'7.1', b'3.0', b'5.9', b'2.1', b'Iris-virginica'],\n",
       "       [b'6.3', b'2.9', b'5.6', b'1.8', b'Iris-virginica'],\n",
       "       [b'6.5', b'3.0', b'5.8', b'2.2', b'Iris-virginica'],\n",
       "       [b'7.6', b'3.0', b'6.6', b'2.1', b'Iris-virginica'],\n",
       "       [b'4.9', b'2.5', b'4.5', b'1.7', b'Iris-virginica'],\n",
       "       [b'7.3', b'2.9', b'6.3', b'1.8', b'Iris-virginica'],\n",
       "       [b'6.7', b'2.5', b'5.8', b'1.8', b'Iris-virginica'],\n",
       "       [b'7.2', b'3.6', b'6.1', b'2.5', b'Iris-virginica'],\n",
       "       [b'6.5', b'3.2', b'5.1', b'2.0', b'Iris-virginica'],\n",
       "       [b'6.4', b'2.7', b'5.3', b'1.9', b'Iris-virginica'],\n",
       "       [b'6.8', b'3.0', b'5.5', b'2.1', b'Iris-virginica'],\n",
       "       [b'5.7', b'2.5', b'5.0', b'2.0', b'Iris-virginica'],\n",
       "       [b'5.8', b'2.8', b'5.1', b'2.4', b'Iris-virginica'],\n",
       "       [b'6.4', b'3.2', b'5.3', b'2.3', b'Iris-virginica'],\n",
       "       [b'6.5', b'3.0', b'5.5', b'1.8', b'Iris-virginica'],\n",
       "       [b'7.7', b'3.8', b'6.7', b'2.2', b'Iris-virginica'],\n",
       "       [b'7.7', b'2.6', b'6.9', b'2.3', b'Iris-virginica'],\n",
       "       [b'6.0', b'2.2', b'5.0', b'1.5', b'Iris-virginica'],\n",
       "       [b'6.9', b'3.2', b'5.7', b'2.3', b'Iris-virginica'],\n",
       "       [b'5.6', b'2.8', b'4.9', b'2.0', b'Iris-virginica'],\n",
       "       [b'7.7', b'2.8', b'6.7', b'2.0', b'Iris-virginica'],\n",
       "       [b'6.3', b'2.7', b'4.9', b'1.8', b'Iris-virginica'],\n",
       "       [b'6.7', b'3.3', b'5.7', b'2.1', b'Iris-virginica'],\n",
       "       [b'7.2', b'3.2', b'6.0', b'1.8', b'Iris-virginica'],\n",
       "       [b'6.2', b'2.8', b'4.8', b'1.8', b'Iris-virginica'],\n",
       "       [b'6.1', b'3.0', b'4.9', b'1.8', b'Iris-virginica'],\n",
       "       [b'6.4', b'2.8', b'5.6', b'2.1', b'Iris-virginica'],\n",
       "       [b'7.2', b'3.0', b'5.8', b'1.6', b'Iris-virginica'],\n",
       "       [b'7.4', b'2.8', b'6.1', b'1.9', b'Iris-virginica'],\n",
       "       [b'7.9', b'3.8', b'6.4', b'2.0', b'Iris-virginica'],\n",
       "       [b'6.4', b'2.8', b'5.6', b'2.2', b'Iris-virginica'],\n",
       "       [b'6.3', b'2.8', b'5.1', b'1.5', b'Iris-virginica'],\n",
       "       [b'6.1', b'2.6', b'5.6', b'1.4', b'Iris-virginica'],\n",
       "       [b'7.7', b'3.0', b'6.1', b'2.3', b'Iris-virginica'],\n",
       "       [b'6.3', b'3.4', b'5.6', b'2.4', b'Iris-virginica'],\n",
       "       [b'6.4', b'3.1', b'5.5', b'1.8', b'Iris-virginica'],\n",
       "       [b'6.0', b'3.0', b'4.8', b'1.8', b'Iris-virginica'],\n",
       "       [b'6.9', b'3.1', b'5.4', b'2.1', b'Iris-virginica'],\n",
       "       [b'6.7', b'3.1', b'5.6', b'2.4', b'Iris-virginica'],\n",
       "       [b'6.9', b'3.1', b'5.1', b'2.3', b'Iris-virginica'],\n",
       "       [b'5.8', b'2.7', b'5.1', b'1.9', b'Iris-virginica'],\n",
       "       [b'6.8', b'3.2', b'5.9', b'2.3', b'Iris-virginica'],\n",
       "       [b'6.7', b'3.3', b'5.7', b'2.5', b'Iris-virginica'],\n",
       "       [b'6.7', b'3.0', b'5.2', b'2.3', b'Iris-virginica'],\n",
       "       [b'6.3', b'2.5', b'5.0', b'1.9', b'Iris-virginica'],\n",
       "       [b'6.5', b'3.0', b'5.2', b'2.0', b'Iris-virginica'],\n",
       "       [b'6.2', b'3.4', b'5.4', b'2.3', b'Iris-virginica'],\n",
       "       [b'5.9', b'3.0', b'5.1', b'1.8', b'Iris-virginica']], dtype=object)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'\n",
    "iris = np.genfromtxt(url, delimiter = ',', dtype = 'object')\n",
    "names = ('sepallength', 'sepalwidth', 'petallength', 'petalwidth', 'species')\n",
    "\n",
    "iris"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 26、如何从一维元组数组中提取特定列\n",
    "\n",
    "**难度等级：** L2\n",
    "\n",
    "**问题：** 从前面问题中导入的一维鸢尾属植物数据集中提取文本列的物种"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(150, 5)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([b'Iris-setosa', b'Iris-setosa', b'Iris-setosa', b'Iris-setosa',\n",
       "       b'Iris-setosa', b'Iris-setosa', b'Iris-setosa', b'Iris-setosa',\n",
       "       b'Iris-setosa', b'Iris-setosa', b'Iris-setosa', b'Iris-setosa',\n",
       "       b'Iris-setosa', b'Iris-setosa', b'Iris-setosa', b'Iris-setosa',\n",
       "       b'Iris-setosa', b'Iris-setosa', b'Iris-setosa', b'Iris-setosa',\n",
       "       b'Iris-setosa', b'Iris-setosa', b'Iris-setosa', b'Iris-setosa',\n",
       "       b'Iris-setosa', b'Iris-setosa', b'Iris-setosa', b'Iris-setosa',\n",
       "       b'Iris-setosa', b'Iris-setosa', b'Iris-setosa', b'Iris-setosa',\n",
       "       b'Iris-setosa', b'Iris-setosa', b'Iris-setosa', b'Iris-setosa',\n",
       "       b'Iris-setosa', b'Iris-setosa', b'Iris-setosa', b'Iris-setosa',\n",
       "       b'Iris-setosa', b'Iris-setosa', b'Iris-setosa', b'Iris-setosa',\n",
       "       b'Iris-setosa', b'Iris-setosa', b'Iris-setosa', b'Iris-setosa',\n",
       "       b'Iris-setosa', b'Iris-setosa', b'Iris-versicolor',\n",
       "       b'Iris-versicolor', b'Iris-versicolor', b'Iris-versicolor',\n",
       "       b'Iris-versicolor', b'Iris-versicolor', b'Iris-versicolor',\n",
       "       b'Iris-versicolor', b'Iris-versicolor', b'Iris-versicolor',\n",
       "       b'Iris-versicolor', b'Iris-versicolor', b'Iris-versicolor',\n",
       "       b'Iris-versicolor', b'Iris-versicolor', b'Iris-versicolor',\n",
       "       b'Iris-versicolor', b'Iris-versicolor', b'Iris-versicolor',\n",
       "       b'Iris-versicolor', b'Iris-versicolor', b'Iris-versicolor',\n",
       "       b'Iris-versicolor', b'Iris-versicolor', b'Iris-versicolor',\n",
       "       b'Iris-versicolor', b'Iris-versicolor', b'Iris-versicolor',\n",
       "       b'Iris-versicolor', b'Iris-versicolor', b'Iris-versicolor',\n",
       "       b'Iris-versicolor', b'Iris-versicolor', b'Iris-versicolor',\n",
       "       b'Iris-versicolor', b'Iris-versicolor', b'Iris-versicolor',\n",
       "       b'Iris-versicolor', b'Iris-versicolor', b'Iris-versicolor',\n",
       "       b'Iris-versicolor', b'Iris-versicolor', b'Iris-versicolor',\n",
       "       b'Iris-versicolor', b'Iris-versicolor', b'Iris-versicolor',\n",
       "       b'Iris-versicolor', b'Iris-versicolor', b'Iris-versicolor',\n",
       "       b'Iris-versicolor', b'Iris-virginica', b'Iris-virginica',\n",
       "       b'Iris-virginica', b'Iris-virginica', b'Iris-virginica',\n",
       "       b'Iris-virginica', b'Iris-virginica', b'Iris-virginica',\n",
       "       b'Iris-virginica', b'Iris-virginica', b'Iris-virginica',\n",
       "       b'Iris-virginica', b'Iris-virginica', b'Iris-virginica',\n",
       "       b'Iris-virginica', b'Iris-virginica', b'Iris-virginica',\n",
       "       b'Iris-virginica', b'Iris-virginica', b'Iris-virginica',\n",
       "       b'Iris-virginica', b'Iris-virginica', b'Iris-virginica',\n",
       "       b'Iris-virginica', b'Iris-virginica', b'Iris-virginica',\n",
       "       b'Iris-virginica', b'Iris-virginica', b'Iris-virginica',\n",
       "       b'Iris-virginica', b'Iris-virginica', b'Iris-virginica',\n",
       "       b'Iris-virginica', b'Iris-virginica', b'Iris-virginica',\n",
       "       b'Iris-virginica', b'Iris-virginica', b'Iris-virginica',\n",
       "       b'Iris-virginica', b'Iris-virginica', b'Iris-virginica',\n",
       "       b'Iris-virginica', b'Iris-virginica', b'Iris-virginica',\n",
       "       b'Iris-virginica', b'Iris-virginica', b'Iris-virginica',\n",
       "       b'Iris-virginica', b'Iris-virginica', b'Iris-virginica'],\n",
       "      dtype='|S15')"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print (iris.shape)\n",
    "\n",
    "species = np.array([row[4] for row in iris])\n",
    "species"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 27、如何将一维元组数组转换为二维 numpy 数组？\n",
    "\n",
    "**难度等级：** L2\n",
    "\n",
    "**问题：** 通过省略鸢尾属植物数据集种类的文本字段，将一维鸢尾属植物数据集转换为二维数组 iris_2d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[b'5.1', b'3.5', b'1.4', b'0.2'],\n",
       "       [b'4.9', b'3.0', b'1.4', b'0.2'],\n",
       "       [b'4.7', b'3.2', b'1.3', b'0.2'],\n",
       "       [b'4.6', b'3.1', b'1.5', b'0.2'],\n",
       "       [b'5.0', b'3.6', b'1.4', b'0.2'],\n",
       "       [b'5.4', b'3.9', b'1.7', b'0.4'],\n",
       "       [b'4.6', b'3.4', b'1.4', b'0.3'],\n",
       "       [b'5.0', b'3.4', b'1.5', b'0.2'],\n",
       "       [b'4.4', b'2.9', b'1.4', b'0.2'],\n",
       "       [b'4.9', b'3.1', b'1.5', b'0.1'],\n",
       "       [b'5.4', b'3.7', b'1.5', b'0.2'],\n",
       "       [b'4.8', b'3.4', b'1.6', b'0.2'],\n",
       "       [b'4.8', b'3.0', b'1.4', b'0.1'],\n",
       "       [b'4.3', b'3.0', b'1.1', b'0.1'],\n",
       "       [b'5.8', b'4.0', b'1.2', b'0.2'],\n",
       "       [b'5.7', b'4.4', b'1.5', b'0.4'],\n",
       "       [b'5.4', b'3.9', b'1.3', b'0.4'],\n",
       "       [b'5.1', b'3.5', b'1.4', b'0.3'],\n",
       "       [b'5.7', b'3.8', b'1.7', b'0.3'],\n",
       "       [b'5.1', b'3.8', b'1.5', b'0.3'],\n",
       "       [b'5.4', b'3.4', b'1.7', b'0.2'],\n",
       "       [b'5.1', b'3.7', b'1.5', b'0.4'],\n",
       "       [b'4.6', b'3.6', b'1.0', b'0.2'],\n",
       "       [b'5.1', b'3.3', b'1.7', b'0.5'],\n",
       "       [b'4.8', b'3.4', b'1.9', b'0.2'],\n",
       "       [b'5.0', b'3.0', b'1.6', b'0.2'],\n",
       "       [b'5.0', b'3.4', b'1.6', b'0.4'],\n",
       "       [b'5.2', b'3.5', b'1.5', b'0.2'],\n",
       "       [b'5.2', b'3.4', b'1.4', b'0.2'],\n",
       "       [b'4.7', b'3.2', b'1.6', b'0.2'],\n",
       "       [b'4.8', b'3.1', b'1.6', b'0.2'],\n",
       "       [b'5.4', b'3.4', b'1.5', b'0.4'],\n",
       "       [b'5.2', b'4.1', b'1.5', b'0.1'],\n",
       "       [b'5.5', b'4.2', b'1.4', b'0.2'],\n",
       "       [b'4.9', b'3.1', b'1.5', b'0.1'],\n",
       "       [b'5.0', b'3.2', b'1.2', b'0.2'],\n",
       "       [b'5.5', b'3.5', b'1.3', b'0.2'],\n",
       "       [b'4.9', b'3.1', b'1.5', b'0.1'],\n",
       "       [b'4.4', b'3.0', b'1.3', b'0.2'],\n",
       "       [b'5.1', b'3.4', b'1.5', b'0.2'],\n",
       "       [b'5.0', b'3.5', b'1.3', b'0.3'],\n",
       "       [b'4.5', b'2.3', b'1.3', b'0.3'],\n",
       "       [b'4.4', b'3.2', b'1.3', b'0.2'],\n",
       "       [b'5.0', b'3.5', b'1.6', b'0.6'],\n",
       "       [b'5.1', b'3.8', b'1.9', b'0.4'],\n",
       "       [b'4.8', b'3.0', b'1.4', b'0.3'],\n",
       "       [b'5.1', b'3.8', b'1.6', b'0.2'],\n",
       "       [b'4.6', b'3.2', b'1.4', b'0.2'],\n",
       "       [b'5.3', b'3.7', b'1.5', b'0.2'],\n",
       "       [b'5.0', b'3.3', b'1.4', b'0.2'],\n",
       "       [b'7.0', b'3.2', b'4.7', b'1.4'],\n",
       "       [b'6.4', b'3.2', b'4.5', b'1.5'],\n",
       "       [b'6.9', b'3.1', b'4.9', b'1.5'],\n",
       "       [b'5.5', b'2.3', b'4.0', b'1.3'],\n",
       "       [b'6.5', b'2.8', b'4.6', b'1.5'],\n",
       "       [b'5.7', b'2.8', b'4.5', b'1.3'],\n",
       "       [b'6.3', b'3.3', b'4.7', b'1.6'],\n",
       "       [b'4.9', b'2.4', b'3.3', b'1.0'],\n",
       "       [b'6.6', b'2.9', b'4.6', b'1.3'],\n",
       "       [b'5.2', b'2.7', b'3.9', b'1.4'],\n",
       "       [b'5.0', b'2.0', b'3.5', b'1.0'],\n",
       "       [b'5.9', b'3.0', b'4.2', b'1.5'],\n",
       "       [b'6.0', b'2.2', b'4.0', b'1.0'],\n",
       "       [b'6.1', b'2.9', b'4.7', b'1.4'],\n",
       "       [b'5.6', b'2.9', b'3.6', b'1.3'],\n",
       "       [b'6.7', b'3.1', b'4.4', b'1.4'],\n",
       "       [b'5.6', b'3.0', b'4.5', b'1.5'],\n",
       "       [b'5.8', b'2.7', b'4.1', b'1.0'],\n",
       "       [b'6.2', b'2.2', b'4.5', b'1.5'],\n",
       "       [b'5.6', b'2.5', b'3.9', b'1.1'],\n",
       "       [b'5.9', b'3.2', b'4.8', b'1.8'],\n",
       "       [b'6.1', b'2.8', b'4.0', b'1.3'],\n",
       "       [b'6.3', b'2.5', b'4.9', b'1.5'],\n",
       "       [b'6.1', b'2.8', b'4.7', b'1.2'],\n",
       "       [b'6.4', b'2.9', b'4.3', b'1.3'],\n",
       "       [b'6.6', b'3.0', b'4.4', b'1.4'],\n",
       "       [b'6.8', b'2.8', b'4.8', b'1.4'],\n",
       "       [b'6.7', b'3.0', b'5.0', b'1.7'],\n",
       "       [b'6.0', b'2.9', b'4.5', b'1.5'],\n",
       "       [b'5.7', b'2.6', b'3.5', b'1.0'],\n",
       "       [b'5.5', b'2.4', b'3.8', b'1.1'],\n",
       "       [b'5.5', b'2.4', b'3.7', b'1.0'],\n",
       "       [b'5.8', b'2.7', b'3.9', b'1.2'],\n",
       "       [b'6.0', b'2.7', b'5.1', b'1.6'],\n",
       "       [b'5.4', b'3.0', b'4.5', b'1.5'],\n",
       "       [b'6.0', b'3.4', b'4.5', b'1.6'],\n",
       "       [b'6.7', b'3.1', b'4.7', b'1.5'],\n",
       "       [b'6.3', b'2.3', b'4.4', b'1.3'],\n",
       "       [b'5.6', b'3.0', b'4.1', b'1.3'],\n",
       "       [b'5.5', b'2.5', b'4.0', b'1.3'],\n",
       "       [b'5.5', b'2.6', b'4.4', b'1.2'],\n",
       "       [b'6.1', b'3.0', b'4.6', b'1.4'],\n",
       "       [b'5.8', b'2.6', b'4.0', b'1.2'],\n",
       "       [b'5.0', b'2.3', b'3.3', b'1.0'],\n",
       "       [b'5.6', b'2.7', b'4.2', b'1.3'],\n",
       "       [b'5.7', b'3.0', b'4.2', b'1.2'],\n",
       "       [b'5.7', b'2.9', b'4.2', b'1.3'],\n",
       "       [b'6.2', b'2.9', b'4.3', b'1.3'],\n",
       "       [b'5.1', b'2.5', b'3.0', b'1.1'],\n",
       "       [b'5.7', b'2.8', b'4.1', b'1.3'],\n",
       "       [b'6.3', b'3.3', b'6.0', b'2.5'],\n",
       "       [b'5.8', b'2.7', b'5.1', b'1.9'],\n",
       "       [b'7.1', b'3.0', b'5.9', b'2.1'],\n",
       "       [b'6.3', b'2.9', b'5.6', b'1.8'],\n",
       "       [b'6.5', b'3.0', b'5.8', b'2.2'],\n",
       "       [b'7.6', b'3.0', b'6.6', b'2.1'],\n",
       "       [b'4.9', b'2.5', b'4.5', b'1.7'],\n",
       "       [b'7.3', b'2.9', b'6.3', b'1.8'],\n",
       "       [b'6.7', b'2.5', b'5.8', b'1.8'],\n",
       "       [b'7.2', b'3.6', b'6.1', b'2.5'],\n",
       "       [b'6.5', b'3.2', b'5.1', b'2.0'],\n",
       "       [b'6.4', b'2.7', b'5.3', b'1.9'],\n",
       "       [b'6.8', b'3.0', b'5.5', b'2.1'],\n",
       "       [b'5.7', b'2.5', b'5.0', b'2.0'],\n",
       "       [b'5.8', b'2.8', b'5.1', b'2.4'],\n",
       "       [b'6.4', b'3.2', b'5.3', b'2.3'],\n",
       "       [b'6.5', b'3.0', b'5.5', b'1.8'],\n",
       "       [b'7.7', b'3.8', b'6.7', b'2.2'],\n",
       "       [b'7.7', b'2.6', b'6.9', b'2.3'],\n",
       "       [b'6.0', b'2.2', b'5.0', b'1.5'],\n",
       "       [b'6.9', b'3.2', b'5.7', b'2.3'],\n",
       "       [b'5.6', b'2.8', b'4.9', b'2.0'],\n",
       "       [b'7.7', b'2.8', b'6.7', b'2.0'],\n",
       "       [b'6.3', b'2.7', b'4.9', b'1.8'],\n",
       "       [b'6.7', b'3.3', b'5.7', b'2.1'],\n",
       "       [b'7.2', b'3.2', b'6.0', b'1.8'],\n",
       "       [b'6.2', b'2.8', b'4.8', b'1.8'],\n",
       "       [b'6.1', b'3.0', b'4.9', b'1.8'],\n",
       "       [b'6.4', b'2.8', b'5.6', b'2.1'],\n",
       "       [b'7.2', b'3.0', b'5.8', b'1.6'],\n",
       "       [b'7.4', b'2.8', b'6.1', b'1.9'],\n",
       "       [b'7.9', b'3.8', b'6.4', b'2.0'],\n",
       "       [b'6.4', b'2.8', b'5.6', b'2.2'],\n",
       "       [b'6.3', b'2.8', b'5.1', b'1.5'],\n",
       "       [b'6.1', b'2.6', b'5.6', b'1.4'],\n",
       "       [b'7.7', b'3.0', b'6.1', b'2.3'],\n",
       "       [b'6.3', b'3.4', b'5.6', b'2.4'],\n",
       "       [b'6.4', b'3.1', b'5.5', b'1.8'],\n",
       "       [b'6.0', b'3.0', b'4.8', b'1.8'],\n",
       "       [b'6.9', b'3.1', b'5.4', b'2.1'],\n",
       "       [b'6.7', b'3.1', b'5.6', b'2.4'],\n",
       "       [b'6.9', b'3.1', b'5.1', b'2.3'],\n",
       "       [b'5.8', b'2.7', b'5.1', b'1.9'],\n",
       "       [b'6.8', b'3.2', b'5.9', b'2.3'],\n",
       "       [b'6.7', b'3.3', b'5.7', b'2.5'],\n",
       "       [b'6.7', b'3.0', b'5.2', b'2.3'],\n",
       "       [b'6.3', b'2.5', b'5.0', b'1.9'],\n",
       "       [b'6.5', b'3.0', b'5.2', b'2.0'],\n",
       "       [b'6.2', b'3.4', b'5.4', b'2.3'],\n",
       "       [b'5.9', b'3.0', b'5.1', b'1.8']], dtype='|S3')"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iris_2d = np.array([row.tolist()[:4] for row in iris])\n",
    "iris_2d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5.1, 3.5, 1.4, 0.2],\n",
       "       [4.9, 3. , 1.4, 0.2],\n",
       "       [4.7, 3.2, 1.3, 0.2],\n",
       "       [4.6, 3.1, 1.5, 0.2],\n",
       "       [5. , 3.6, 1.4, 0.2],\n",
       "       [5.4, 3.9, 1.7, 0.4],\n",
       "       [4.6, 3.4, 1.4, 0.3],\n",
       "       [5. , 3.4, 1.5, 0.2],\n",
       "       [4.4, 2.9, 1.4, 0.2],\n",
       "       [4.9, 3.1, 1.5, 0.1],\n",
       "       [5.4, 3.7, 1.5, 0.2],\n",
       "       [4.8, 3.4, 1.6, 0.2],\n",
       "       [4.8, 3. , 1.4, 0.1],\n",
       "       [4.3, 3. , 1.1, 0.1],\n",
       "       [5.8, 4. , 1.2, 0.2],\n",
       "       [5.7, 4.4, 1.5, 0.4],\n",
       "       [5.4, 3.9, 1.3, 0.4],\n",
       "       [5.1, 3.5, 1.4, 0.3],\n",
       "       [5.7, 3.8, 1.7, 0.3],\n",
       "       [5.1, 3.8, 1.5, 0.3],\n",
       "       [5.4, 3.4, 1.7, 0.2],\n",
       "       [5.1, 3.7, 1.5, 0.4],\n",
       "       [4.6, 3.6, 1. , 0.2],\n",
       "       [5.1, 3.3, 1.7, 0.5],\n",
       "       [4.8, 3.4, 1.9, 0.2],\n",
       "       [5. , 3. , 1.6, 0.2],\n",
       "       [5. , 3.4, 1.6, 0.4],\n",
       "       [5.2, 3.5, 1.5, 0.2],\n",
       "       [5.2, 3.4, 1.4, 0.2],\n",
       "       [4.7, 3.2, 1.6, 0.2],\n",
       "       [4.8, 3.1, 1.6, 0.2],\n",
       "       [5.4, 3.4, 1.5, 0.4],\n",
       "       [5.2, 4.1, 1.5, 0.1],\n",
       "       [5.5, 4.2, 1.4, 0.2],\n",
       "       [4.9, 3.1, 1.5, 0.1],\n",
       "       [5. , 3.2, 1.2, 0.2],\n",
       "       [5.5, 3.5, 1.3, 0.2],\n",
       "       [4.9, 3.1, 1.5, 0.1],\n",
       "       [4.4, 3. , 1.3, 0.2],\n",
       "       [5.1, 3.4, 1.5, 0.2],\n",
       "       [5. , 3.5, 1.3, 0.3],\n",
       "       [4.5, 2.3, 1.3, 0.3],\n",
       "       [4.4, 3.2, 1.3, 0.2],\n",
       "       [5. , 3.5, 1.6, 0.6],\n",
       "       [5.1, 3.8, 1.9, 0.4],\n",
       "       [4.8, 3. , 1.4, 0.3],\n",
       "       [5.1, 3.8, 1.6, 0.2],\n",
       "       [4.6, 3.2, 1.4, 0.2],\n",
       "       [5.3, 3.7, 1.5, 0.2],\n",
       "       [5. , 3.3, 1.4, 0.2],\n",
       "       [7. , 3.2, 4.7, 1.4],\n",
       "       [6.4, 3.2, 4.5, 1.5],\n",
       "       [6.9, 3.1, 4.9, 1.5],\n",
       "       [5.5, 2.3, 4. , 1.3],\n",
       "       [6.5, 2.8, 4.6, 1.5],\n",
       "       [5.7, 2.8, 4.5, 1.3],\n",
       "       [6.3, 3.3, 4.7, 1.6],\n",
       "       [4.9, 2.4, 3.3, 1. ],\n",
       "       [6.6, 2.9, 4.6, 1.3],\n",
       "       [5.2, 2.7, 3.9, 1.4],\n",
       "       [5. , 2. , 3.5, 1. ],\n",
       "       [5.9, 3. , 4.2, 1.5],\n",
       "       [6. , 2.2, 4. , 1. ],\n",
       "       [6.1, 2.9, 4.7, 1.4],\n",
       "       [5.6, 2.9, 3.6, 1.3],\n",
       "       [6.7, 3.1, 4.4, 1.4],\n",
       "       [5.6, 3. , 4.5, 1.5],\n",
       "       [5.8, 2.7, 4.1, 1. ],\n",
       "       [6.2, 2.2, 4.5, 1.5],\n",
       "       [5.6, 2.5, 3.9, 1.1],\n",
       "       [5.9, 3.2, 4.8, 1.8],\n",
       "       [6.1, 2.8, 4. , 1.3],\n",
       "       [6.3, 2.5, 4.9, 1.5],\n",
       "       [6.1, 2.8, 4.7, 1.2],\n",
       "       [6.4, 2.9, 4.3, 1.3],\n",
       "       [6.6, 3. , 4.4, 1.4],\n",
       "       [6.8, 2.8, 4.8, 1.4],\n",
       "       [6.7, 3. , 5. , 1.7],\n",
       "       [6. , 2.9, 4.5, 1.5],\n",
       "       [5.7, 2.6, 3.5, 1. ],\n",
       "       [5.5, 2.4, 3.8, 1.1],\n",
       "       [5.5, 2.4, 3.7, 1. ],\n",
       "       [5.8, 2.7, 3.9, 1.2],\n",
       "       [6. , 2.7, 5.1, 1.6],\n",
       "       [5.4, 3. , 4.5, 1.5],\n",
       "       [6. , 3.4, 4.5, 1.6],\n",
       "       [6.7, 3.1, 4.7, 1.5],\n",
       "       [6.3, 2.3, 4.4, 1.3],\n",
       "       [5.6, 3. , 4.1, 1.3],\n",
       "       [5.5, 2.5, 4. , 1.3],\n",
       "       [5.5, 2.6, 4.4, 1.2],\n",
       "       [6.1, 3. , 4.6, 1.4],\n",
       "       [5.8, 2.6, 4. , 1.2],\n",
       "       [5. , 2.3, 3.3, 1. ],\n",
       "       [5.6, 2.7, 4.2, 1.3],\n",
       "       [5.7, 3. , 4.2, 1.2],\n",
       "       [5.7, 2.9, 4.2, 1.3],\n",
       "       [6.2, 2.9, 4.3, 1.3],\n",
       "       [5.1, 2.5, 3. , 1.1],\n",
       "       [5.7, 2.8, 4.1, 1.3],\n",
       "       [6.3, 3.3, 6. , 2.5],\n",
       "       [5.8, 2.7, 5.1, 1.9],\n",
       "       [7.1, 3. , 5.9, 2.1],\n",
       "       [6.3, 2.9, 5.6, 1.8],\n",
       "       [6.5, 3. , 5.8, 2.2],\n",
       "       [7.6, 3. , 6.6, 2.1],\n",
       "       [4.9, 2.5, 4.5, 1.7],\n",
       "       [7.3, 2.9, 6.3, 1.8],\n",
       "       [6.7, 2.5, 5.8, 1.8],\n",
       "       [7.2, 3.6, 6.1, 2.5],\n",
       "       [6.5, 3.2, 5.1, 2. ],\n",
       "       [6.4, 2.7, 5.3, 1.9],\n",
       "       [6.8, 3. , 5.5, 2.1],\n",
       "       [5.7, 2.5, 5. , 2. ],\n",
       "       [5.8, 2.8, 5.1, 2.4],\n",
       "       [6.4, 3.2, 5.3, 2.3],\n",
       "       [6.5, 3. , 5.5, 1.8],\n",
       "       [7.7, 3.8, 6.7, 2.2],\n",
       "       [7.7, 2.6, 6.9, 2.3],\n",
       "       [6. , 2.2, 5. , 1.5],\n",
       "       [6.9, 3.2, 5.7, 2.3],\n",
       "       [5.6, 2.8, 4.9, 2. ],\n",
       "       [7.7, 2.8, 6.7, 2. ],\n",
       "       [6.3, 2.7, 4.9, 1.8],\n",
       "       [6.7, 3.3, 5.7, 2.1],\n",
       "       [7.2, 3.2, 6. , 1.8],\n",
       "       [6.2, 2.8, 4.8, 1.8],\n",
       "       [6.1, 3. , 4.9, 1.8],\n",
       "       [6.4, 2.8, 5.6, 2.1],\n",
       "       [7.2, 3. , 5.8, 1.6],\n",
       "       [7.4, 2.8, 6.1, 1.9],\n",
       "       [7.9, 3.8, 6.4, 2. ],\n",
       "       [6.4, 2.8, 5.6, 2.2],\n",
       "       [6.3, 2.8, 5.1, 1.5],\n",
       "       [6.1, 2.6, 5.6, 1.4],\n",
       "       [7.7, 3. , 6.1, 2.3],\n",
       "       [6.3, 3.4, 5.6, 2.4],\n",
       "       [6.4, 3.1, 5.5, 1.8],\n",
       "       [6. , 3. , 4.8, 1.8],\n",
       "       [6.9, 3.1, 5.4, 2.1],\n",
       "       [6.7, 3.1, 5.6, 2.4],\n",
       "       [6.9, 3.1, 5.1, 2.3],\n",
       "       [5.8, 2.7, 5.1, 1.9],\n",
       "       [6.8, 3.2, 5.9, 2.3],\n",
       "       [6.7, 3.3, 5.7, 2.5],\n",
       "       [6.7, 3. , 5.2, 2.3],\n",
       "       [6.3, 2.5, 5. , 1.9],\n",
       "       [6.5, 3. , 5.2, 2. ],\n",
       "       [6.2, 3.4, 5.4, 2.3],\n",
       "       [5.9, 3. , 5.1, 1.8]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iris_2d = np.genfromtxt(url, delimiter = ',', dtype = 'float', usecols = [0, 1, 2, 3])\n",
    "iris_2d"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 28、如何计算 numpy 数组的均值，中位数，标准差？\n",
    "\n",
    "**难度等级：** L1\n",
    "\n",
    "**问题：** 求出鸢尾属植物萼片长度的平均值、中位数和标准差（第 1 列）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5.843333333333334 5.8 0.8253012917851409\n"
     ]
    }
   ],
   "source": [
    "sepallength = np.genfromtxt(url, delimiter = ',', dtype = 'float', usecols = [0])\n",
    "\n",
    "mu, med, sd = np.mean(sepallength), np.median(sepallength), np.std(sepallength)\n",
    "\n",
    "print(mu, med, sd)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 29、如何规范化数组，使数组的值正好介于 0 和 1 之间？\n",
    "\n",
    "**难度等级：** L2\n",
    "\n",
    "**问题：** 创建一种规范化形式的鸢尾属植物间隔长度，其值正好介于 0 和 1 之间，这样最小值为 0，最大值为 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.222222 0.166667 0.111111 0.083333 0.194444 0.305556 0.083333 0.194444\n",
      " 0.027778 0.166667 0.305556 0.138889 0.138889 0.       0.416667 0.388889\n",
      " 0.305556 0.222222 0.388889 0.222222 0.305556 0.222222 0.083333 0.222222\n",
      " 0.138889 0.194444 0.194444 0.25     0.25     0.111111 0.138889 0.305556\n",
      " 0.25     0.333333 0.166667 0.194444 0.333333 0.166667 0.027778 0.222222\n",
      " 0.194444 0.055556 0.027778 0.194444 0.222222 0.138889 0.222222 0.083333\n",
      " 0.277778 0.194444 0.75     0.583333 0.722222 0.333333 0.611111 0.388889\n",
      " 0.555556 0.166667 0.638889 0.25     0.194444 0.444444 0.472222 0.5\n",
      " 0.361111 0.666667 0.361111 0.416667 0.527778 0.361111 0.444444 0.5\n",
      " 0.555556 0.5      0.583333 0.638889 0.694444 0.666667 0.472222 0.388889\n",
      " 0.333333 0.333333 0.416667 0.472222 0.305556 0.472222 0.666667 0.555556\n",
      " 0.361111 0.333333 0.333333 0.5      0.416667 0.194444 0.361111 0.388889\n",
      " 0.388889 0.527778 0.222222 0.388889 0.555556 0.416667 0.777778 0.555556\n",
      " 0.611111 0.916667 0.166667 0.833333 0.666667 0.805556 0.611111 0.583333\n",
      " 0.694444 0.388889 0.416667 0.583333 0.611111 0.944444 0.944444 0.472222\n",
      " 0.722222 0.361111 0.944444 0.555556 0.666667 0.805556 0.527778 0.5\n",
      " 0.583333 0.805556 0.861111 1.       0.583333 0.555556 0.5      0.944444\n",
      " 0.555556 0.583333 0.472222 0.722222 0.666667 0.722222 0.416667 0.694444\n",
      " 0.666667 0.666667 0.555556 0.611111 0.527778 0.444444]\n",
      "[0.222222 0.166667 0.111111 0.083333 0.194444 0.305556 0.083333 0.194444\n",
      " 0.027778 0.166667 0.305556 0.138889 0.138889 0.       0.416667 0.388889\n",
      " 0.305556 0.222222 0.388889 0.222222 0.305556 0.222222 0.083333 0.222222\n",
      " 0.138889 0.194444 0.194444 0.25     0.25     0.111111 0.138889 0.305556\n",
      " 0.25     0.333333 0.166667 0.194444 0.333333 0.166667 0.027778 0.222222\n",
      " 0.194444 0.055556 0.027778 0.194444 0.222222 0.138889 0.222222 0.083333\n",
      " 0.277778 0.194444 0.75     0.583333 0.722222 0.333333 0.611111 0.388889\n",
      " 0.555556 0.166667 0.638889 0.25     0.194444 0.444444 0.472222 0.5\n",
      " 0.361111 0.666667 0.361111 0.416667 0.527778 0.361111 0.444444 0.5\n",
      " 0.555556 0.5      0.583333 0.638889 0.694444 0.666667 0.472222 0.388889\n",
      " 0.333333 0.333333 0.416667 0.472222 0.305556 0.472222 0.666667 0.555556\n",
      " 0.361111 0.333333 0.333333 0.5      0.416667 0.194444 0.361111 0.388889\n",
      " 0.388889 0.527778 0.222222 0.388889 0.555556 0.416667 0.777778 0.555556\n",
      " 0.611111 0.916667 0.166667 0.833333 0.666667 0.805556 0.611111 0.583333\n",
      " 0.694444 0.388889 0.416667 0.583333 0.611111 0.944444 0.944444 0.472222\n",
      " 0.722222 0.361111 0.944444 0.555556 0.666667 0.805556 0.527778 0.5\n",
      " 0.583333 0.805556 0.861111 1.       0.583333 0.555556 0.5      0.944444\n",
      " 0.555556 0.583333 0.472222 0.722222 0.666667 0.722222 0.416667 0.694444\n",
      " 0.666667 0.666667 0.555556 0.611111 0.527778 0.444444]\n"
     ]
    }
   ],
   "source": [
    "Smax, Smin = sepallength.max(), sepallength.min()\n",
    "S = (sepallength - Smin) / (Smax - Smin)\n",
    "print(S)\n",
    "S = (sepallength - Smin) / sepallength.ptp()\n",
    "print(S)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 30、如何计算 Softmax 得分\n",
    "\n",
    "**难度等级：** L3\n",
    "\n",
    "**问题：** 计算 sepallength 的 softmax 分数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.00222  0.001817 0.001488 0.001346 0.002008 0.002996 0.001346 0.002008\n",
      " 0.001102 0.001817 0.002996 0.001644 0.001644 0.000997 0.00447  0.004044\n",
      " 0.002996 0.00222  0.004044 0.00222  0.002996 0.00222  0.001346 0.00222\n",
      " 0.001644 0.002008 0.002008 0.002453 0.002453 0.001488 0.001644 0.002996\n",
      " 0.002453 0.003311 0.001817 0.002008 0.003311 0.001817 0.001102 0.00222\n",
      " 0.002008 0.001218 0.001102 0.002008 0.00222  0.001644 0.00222  0.001346\n",
      " 0.002711 0.002008 0.01484  0.008144 0.013428 0.003311 0.009001 0.004044\n",
      " 0.007369 0.001817 0.009947 0.002453 0.002008 0.00494  0.005459 0.006033\n",
      " 0.003659 0.010994 0.003659 0.00447  0.006668 0.003659 0.00494  0.006033\n",
      " 0.007369 0.006033 0.008144 0.009947 0.01215  0.010994 0.005459 0.004044\n",
      " 0.003311 0.003311 0.00447  0.005459 0.002996 0.005459 0.010994 0.007369\n",
      " 0.003659 0.003311 0.003311 0.006033 0.00447  0.002008 0.003659 0.004044\n",
      " 0.004044 0.006668 0.00222  0.004044 0.007369 0.00447  0.016401 0.007369\n",
      " 0.009001 0.02704  0.001817 0.020032 0.010994 0.018126 0.009001 0.008144\n",
      " 0.01215  0.004044 0.00447  0.008144 0.009001 0.029884 0.029884 0.005459\n",
      " 0.013428 0.003659 0.029884 0.007369 0.010994 0.018126 0.006668 0.006033\n",
      " 0.008144 0.018126 0.022139 0.0365   0.008144 0.007369 0.006033 0.029884\n",
      " 0.007369 0.008144 0.005459 0.013428 0.010994 0.013428 0.00447  0.01215\n",
      " 0.010994 0.010994 0.007369 0.009001 0.006668 0.00494 ]\n"
     ]
    }
   ],
   "source": [
    "url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'\n",
    "iris = np.genfromtxt(url, delimiter = ',', dtype = 'object')\n",
    "sepallength = np.array([float(row[0]) for row in iris])\n",
    "\n",
    "def Softmax(x):\n",
    "    e_x = np.exp(x - np.max(x))\n",
    "    return e_x / e_x.sum(axis = 0)\n",
    "\n",
    "print(Softmax(sepallength))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 31、如何找到 numpy 数组的百分位数？\n",
    "\n",
    "**难度等级：** L1\n",
    "\n",
    "**问题：** 找到鸢尾属植物数据集的第 5 和第 95 百分位数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4.6  , 7.255])"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'\n",
    "sepallength = np.genfromtxt(url, delimiter = ',', dtype = 'float', usecols = [0])\n",
    "\n",
    "np.percentile(sepallength, q = [5, 95])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 32、如何在数组中的随机位置插入值？\n",
    "\n",
    "**难度等级：** L2\n",
    "\n",
    "**问题：** 在 iris_2d 数据集中的 20 个随机位置插入 np.nan 值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[b'5.1' b'3.5' b'1.4' b'0.2' b'Iris-setosa']\n",
      " [b'4.9' b'3.0' b'1.4' b'0.2' b'Iris-setosa']\n",
      " [b'4.7' b'3.2' b'1.3' b'0.2' b'Iris-setosa']\n",
      " [b'4.6' b'3.1' b'1.5' b'0.2' b'Iris-setosa']\n",
      " [b'5.0' b'3.6' b'1.4' b'0.2' b'Iris-setosa']\n",
      " [b'5.4' b'3.9' b'1.7' b'0.4' b'Iris-setosa']\n",
      " [b'4.6' b'3.4' b'1.4' b'0.3' b'Iris-setosa']\n",
      " [b'5.0' b'3.4' b'1.5' b'0.2' b'Iris-setosa']\n",
      " [b'4.4' b'2.9' b'1.4' b'0.2' b'Iris-setosa']\n",
      " [b'4.9' b'3.1' b'1.5' b'0.1' b'Iris-setosa']]\n",
      "[[b'5.1' b'3.5' b'1.4' b'0.2' b'Iris-setosa']\n",
      " [b'4.9' b'3.0' b'1.4' b'0.2' b'Iris-setosa']\n",
      " [b'4.7' b'3.2' b'1.3' b'0.2' b'Iris-setosa']\n",
      " [b'4.6' b'3.1' b'1.5' b'0.2' b'Iris-setosa']\n",
      " [b'5.0' b'3.6' b'1.4' b'0.2' b'Iris-setosa']\n",
      " [b'5.4' b'3.9' b'1.7' b'0.4' b'Iris-setosa']\n",
      " [b'4.6' b'3.4' b'1.4' b'0.3' b'Iris-setosa']\n",
      " [b'5.0' b'3.4' b'1.5' b'0.2' b'Iris-setosa']\n",
      " [b'4.4' nan b'1.4' b'0.2' b'Iris-setosa']\n",
      " [b'4.9' b'3.1' b'1.5' b'0.1' b'Iris-setosa']]\n"
     ]
    }
   ],
   "source": [
    "url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'\n",
    "iris_2d = np.genfromtxt(url, delimiter = ',', dtype = 'object')\n",
    "\n",
    "# 法一\n",
    "i, j = np. where(iris_2d)\n",
    "\n",
    "np.random.seed(100)\n",
    "iris_2d[np.random.choice((i), 20), np.random.choice((j), 20)] = np.nan\n",
    "\n",
    "print(iris_2d[:10])\n",
    "\n",
    "# 法二\n",
    "np.random.seed(100)\n",
    "iris_2d[np.random.randint(150, size = 20), np.random.randint(4, size = 20)] = np.nan\n",
    "\n",
    "print(iris_2d[:10])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 33、如何在 numpy 数组中找到缺失值的位置？\n",
    "\n",
    "**难度等级：** L2\n",
    "\n",
    "**问题：** 在 iris_2d 的 sepallength 中查找缺失值的数量和位置（第 1 列）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of missing values: \n",
      " 5\n",
      "Position of missing values: \n",
      " (array([  0,  43,  84,  94, 138], dtype=int64),)\n"
     ]
    }
   ],
   "source": [
    "url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'\n",
    "iris_2d = np.genfromtxt(url, delimiter = ',', dtype = 'float')\n",
    "iris_2d[np.random.randint(150, size = 20), np.random.randint(4, size = 20)] = np.nan\n",
    "\n",
    "print(\"Number of missing values: \\n\", np.isnan(iris_2d[:, 0]).sum())\n",
    "print(\"Position of missing values: \\n\", np.where(np.isnan(iris_2d[:, 0])))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 34、如何根据两个或多个条件过滤 numpy 数组？\n",
    "\n",
    "**难度等级：** L3\n",
    "\n",
    "**问题：** 过滤具有 petallength（第 3 列）> 1.5 和 sepallength（第 1 列）< 5.0 的 iris_2d 行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4.8, 3.4, 1.6, 0.2],\n",
       "       [4.8, 3.4, 1.9, 0.2],\n",
       "       [4.7, 3.2, 1.6, 0.2],\n",
       "       [4.8, 3.1, 1.6, 0.2],\n",
       "       [4.9, 2.4, 3.3, 1. ],\n",
       "       [4.9, 2.5, 4.5, 1.7]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'\n",
    "iris_2d = np.genfromtxt(url, delimiter = ',', dtype = 'float', usecols = [0, 1, 2, 3])\n",
    "\n",
    "condition = (iris_2d[:, 2] > 1.5) & (iris_2d[:, 0] < 5.0)\n",
    "iris_2d[condition]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 35、如何从 numpy 数组中删除包含缺失值的行？\n",
    "\n",
    "**难度等级:** L3\n",
    "\n",
    "**问题：** 选择没有任何 nan 值的 iris_2d 行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5.1, 3.5, 1.4, 0.2],\n",
       "       [4.9, 3. , 1.4, 0.2],\n",
       "       [4.7, 3.2, 1.3, 0.2],\n",
       "       [4.6, 3.1, 1.5, 0.2],\n",
       "       [5. , 3.6, 1.4, 0.2]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'\n",
    "iris_2d = np.genfromtxt(url, delimiter = ',', dtype = 'float', usecols = [0, 1, 2, 3])\n",
    "iris_2d[np.random.randint(150, size = 20), np.random.randint(4, size = 20)] = np.nan\n",
    "\n",
    "any_nan_in_row = np.array([~np.any(np.isnan(row)) for row in iris_2d])\n",
    "iris_2d[any_nan_in_row][:5]\n",
    "\n",
    "iris_2d[np.sum(np.isnan(iris_2d), axis = 1) == 0][:5]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 36、如何找到 numpy 数组的两列之间的相关性？\n",
    "\n",
    "**难度等级：** L2\n",
    "\n",
    "**问题：** 在 iris_2d 中找出 SepalLength(第 1 列）和 PetalLength（第 3 列）之间的相关性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.8717541573048718\n",
      "0.8717541573048712\n"
     ]
    }
   ],
   "source": [
    "url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'\n",
    "iris = np.genfromtxt(url, delimiter = ',', dtype = 'float', usecols = [0, 1, 2, 3])\n",
    "\n",
    "# 法一\n",
    "print(np.corrcoef(iris[:, 0], iris[:, 2])[0, 1])\n",
    "\n",
    "# 法二\n",
    "from scipy.stats.stats import pearsonr\n",
    "corr, p_value = pearsonr(iris[:, 0], iris[:, 2])\n",
    "print(corr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 37、如何查找给定数组是否具有任何空值？\n",
    "\n",
    "**难度等级：** L2\n",
    "\n",
    "**问题：** 找出 iris_2d 是否有任何缺失值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'\n",
    "iris_2d = np.genfromtxt(url, delimiter = ',', dtype = 'float', usecols = [0, 1, 2, 3])\n",
    "\n",
    "np.isnan(iris_2d).any()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 38、如何在 numpy 数组中用 0 替换所有缺失值？\n",
    "\n",
    "**难度等级：** L2\n",
    "\n",
    "**问题：** 在 numpy 数组中将所有出现的 nan 替换为 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5.1, 3.5, 1.4, 0.2],\n",
       "       [4.9, 3. , 1.4, 0.2],\n",
       "       [4.7, 0. , 1.3, 0.2],\n",
       "       [4.6, 3.1, 1.5, 0.2]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'\n",
    "iris_2d = np.genfromtxt(url, delimiter = ',', dtype = 'float', usecols = [0, 1, 2, 3])\n",
    "iris_2d[np.random.randint(150, size = 20), np.random.randint(4, size = 20)] = np.nan\n",
    "\n",
    "iris_2d[np.isnan(iris_2d)] = 0\n",
    "iris_2d[:4]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 39、如何在 numpy 数组中查找唯一值的计数？\n",
    "\n",
    "**难度等级：** L2\n",
    "\n",
    "**问题：** 找出鸢尾属植物物种的独特值和独特值的数量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([b'Iris-setosa', b'Iris-versicolor', b'Iris-virginica'],\n",
       "       dtype='|S15'), array([50, 50, 50], dtype=int64))"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'\n",
    "iris = np.genfromtxt(url, delimiter = ',', dtype = 'object')\n",
    "names = ('sepallength', 'sepalwidth', 'petallength', 'petalwidth', 'species')\n",
    "\n",
    "species = np.array([row.tolist()[4] for row in iris])\n",
    "\n",
    "np.unique(species, return_counts = True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 40、如何将数字转换为分类（文本）数组？\n",
    "\n",
    "**难度等级：** L2\n",
    "\n",
    "**问题：** 将 iris_2d 的花瓣长度（第 3 列）加入以形成文本数组，这样如果花瓣长度为：\n",
    "- Less than 3 --> 'small'\n",
    "- 3-5 --> 'medium'\n",
    "- \\>= 5 --> 'large'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['small', 'small', 'small', 'small']"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'\n",
    "iris = np.genfromtxt(url, delimiter = ',', dtype = 'object')\n",
    "names = ('sepallength', 'sepalwidth', 'petallength', 'petalwidth', 'species')\n",
    "\n",
    "petal_length_bin = np.digitize(iris[:, 2].astype('float'), [0, 3, 5, 10])\n",
    "\n",
    "label_map = {1: 'small', 2: 'medium', 3: 'large', 4: np.nan}\n",
    "petal_length_cat = [label_map[x] for x in petal_length_bin]\n",
    "\n",
    "petal_length_cat[:4]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 41、如何从 numpy 数组的现有列创建新列？\n",
    "\n",
    "**难度等级：** L2\n",
    "\n",
    "**问题：** 在 iris_2d 中为卷创建了一个新列，其中 volume 是 (pi x petallength x sepal_length ^ 2) / 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[b'5.1', b'3.5', b'1.4', b'0.2', b'Iris-setosa',\n",
       "        38.13265162927291],\n",
       "       [b'4.9', b'3.0', b'1.4', b'0.2', b'Iris-setosa',\n",
       "        35.200498485922445],\n",
       "       [b'4.7', b'3.2', b'1.3', b'0.2', b'Iris-setosa', 30.0723720777127],\n",
       "       [b'4.6', b'3.1', b'1.5', b'0.2', b'Iris-setosa',\n",
       "        33.238050274980004]], dtype=object)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'\n",
    "iris_2d = np.genfromtxt(url, delimiter = ',', dtype = 'object')\n",
    "\n",
    "sepallength = iris_2d[:, 0].astype('float')\n",
    "petallength = iris_2d[:, 2].astype('float')\n",
    "volume = (np.pi * petallength * (sepallength ** 2)) / 3\n",
    "\n",
    "volume = volume[:, np.newaxis]\n",
    "\n",
    "out = np.hstack([iris_2d, volume])\n",
    "\n",
    "out[:4]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 42、如何在 numpy 中进行概率抽样？\n",
    "\n",
    "**难度等级：** L3\n",
    "\n",
    "**问题：** 随机抽鸢尾属植物的种类，使得刚毛的数量是云芝和维吉尼亚的两倍"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[' Iris-versicolor' 'Iris-setosa' 'Iris-setosa' 'Iris-virginica'\n",
      " 'Iris-setosa' 'Iris-setosa' ' Iris-versicolor' 'Iris-virginica'\n",
      " 'Iris-setosa' ' Iris-versicolor' 'Iris-virginica' 'Iris-setosa'\n",
      " 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-virginica'\n",
      " 'Iris-virginica' 'Iris-setosa' 'Iris-virginica' 'Iris-setosa'\n",
      " 'Iris-setosa' 'Iris-virginica' 'Iris-virginica' 'Iris-setosa'\n",
      " 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-virginica'\n",
      " 'Iris-setosa' ' Iris-versicolor' ' Iris-versicolor' 'Iris-setosa'\n",
      " 'Iris-setosa' 'Iris-setosa' 'Iris-virginica' 'Iris-virginica'\n",
      " 'Iris-setosa' 'Iris-virginica' ' Iris-versicolor' ' Iris-versicolor'\n",
      " ' Iris-versicolor' ' Iris-versicolor' 'Iris-setosa' 'Iris-setosa'\n",
      " ' Iris-versicolor' 'Iris-virginica' 'Iris-setosa' 'Iris-setosa'\n",
      " 'Iris-virginica' 'Iris-virginica' 'Iris-virginica' 'Iris-setosa'\n",
      " ' Iris-versicolor' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa'\n",
      " 'Iris-setosa' 'Iris-setosa' ' Iris-versicolor' 'Iris-setosa'\n",
      " ' Iris-versicolor' ' Iris-versicolor' 'Iris-setosa' 'Iris-virginica'\n",
      " 'Iris-virginica' ' Iris-versicolor' 'Iris-setosa' 'Iris-setosa'\n",
      " 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-virginica'\n",
      " 'Iris-virginica' ' Iris-versicolor' ' Iris-versicolor' 'Iris-setosa'\n",
      " 'Iris-setosa' 'Iris-setosa' ' Iris-versicolor' 'Iris-setosa'\n",
      " ' Iris-versicolor' 'Iris-virginica' 'Iris-setosa' 'Iris-setosa'\n",
      " 'Iris-virginica' 'Iris-virginica' 'Iris-setosa' ' Iris-versicolor'\n",
      " ' Iris-versicolor' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa'\n",
      " 'Iris-setosa' ' Iris-versicolor' 'Iris-setosa' ' Iris-versicolor'\n",
      " 'Iris-setosa' ' Iris-versicolor' 'Iris-virginica' 'Iris-virginica'\n",
      " ' Iris-versicolor' 'Iris-setosa' ' Iris-versicolor' 'Iris-virginica'\n",
      " ' Iris-versicolor' 'Iris-virginica' 'Iris-virginica' 'Iris-setosa'\n",
      " 'Iris-setosa' 'Iris-setosa' 'Iris-virginica' 'Iris-virginica'\n",
      " 'Iris-setosa' 'Iris-setosa' ' Iris-versicolor' 'Iris-setosa'\n",
      " 'Iris-setosa' ' Iris-versicolor' ' Iris-versicolor' ' Iris-versicolor'\n",
      " ' Iris-versicolor' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa'\n",
      " 'Iris-setosa' 'Iris-setosa' ' Iris-versicolor' 'Iris-setosa'\n",
      " 'Iris-setosa' 'Iris-setosa' 'Iris-virginica' 'Iris-setosa' 'Iris-setosa'\n",
      " 'Iris-setosa' ' Iris-versicolor' 'Iris-virginica' 'Iris-virginica'\n",
      " 'Iris-virginica' 'Iris-setosa' ' Iris-versicolor' ' Iris-versicolor'\n",
      " 'Iris-setosa' 'Iris-virginica' 'Iris-setosa' 'Iris-virginica'\n",
      " ' Iris-versicolor' 'Iris-setosa' 'Iris-setosa']\n",
      "(array([b'Iris-setosa', b'Iris-versicolor', b'Iris-virginica'],\n",
      "      dtype=object), array([77, 37, 36], dtype=int64))\n"
     ]
    }
   ],
   "source": [
    "url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'\n",
    "iris = np.genfromtxt(url, delimiter = ',', dtype = 'object')\n",
    "\n",
    "species = iris[:, 4]\n",
    "\n",
    "# 方法一\n",
    "np.random.seed(100)\n",
    "a = np.array(['Iris-setosa', ' Iris-versicolor', 'Iris-virginica'])\n",
    "species_out = np.random.choice(a, 150, p = [0.5, 0.25, 0.25])\n",
    "print(species_out)\n",
    "\n",
    "# 方法二\n",
    "np.random.seed(100)\n",
    "probs = np.r_[np.linspace(0, 0.500, num = 50), np.linspace(0.501, .750, num = 50), np.linspace(.751, 1.0, num = 50)]\n",
    "index = np.searchsorted(probs, np.random.random(150))\n",
    "species_out = species[index]\n",
    "print(np.unique(species_out, return_counts = True))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 43、如何在按另一个数组分组时获取数组的第二大值？\n",
    "\n",
    "**难度等级：** L2\n",
    "\n",
    "**问题：** 第二长的物种 setosa 的价值是多少"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.7"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'\n",
    "iris = np.genfromtxt(url, delimiter = ',', dtype = 'object')\n",
    "\n",
    "petal_len_setosa = iris[iris[:, 4] == b'Iris-setosa', [2]].astype('float')\n",
    "\n",
    "np.unique(np.sort(petal_len_setosa))[-2]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 44、如何按列对 2D 数组进行排序\n",
    "\n",
    "**难度等级：** L2\n",
    "\n",
    "**问题：** 根据 sepallength 列队虹膜数据集进行排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[b'4.3' b'3.0' b'1.1' b'0.1' b'Iris-setosa']\n",
      " [b'4.4' b'3.2' b'1.3' b'0.2' b'Iris-setosa']\n",
      " [b'4.4' b'3.0' b'1.3' b'0.2' b'Iris-setosa']\n",
      " [b'4.4' b'2.9' b'1.4' b'0.2' b'Iris-setosa']\n",
      " [b'4.5' b'2.3' b'1.3' b'0.3' b'Iris-setosa']\n",
      " [b'4.6' b'3.6' b'1.0' b'0.2' b'Iris-setosa']\n",
      " [b'4.6' b'3.1' b'1.5' b'0.2' b'Iris-setosa']\n",
      " [b'4.6' b'3.4' b'1.4' b'0.3' b'Iris-setosa']\n",
      " [b'4.6' b'3.2' b'1.4' b'0.2' b'Iris-setosa']\n",
      " [b'4.7' b'3.2' b'1.3' b'0.2' b'Iris-setosa']\n",
      " [b'4.7' b'3.2' b'1.6' b'0.2' b'Iris-setosa']\n",
      " [b'4.8' b'3.0' b'1.4' b'0.1' b'Iris-setosa']\n",
      " [b'4.8' b'3.0' b'1.4' b'0.3' b'Iris-setosa']\n",
      " [b'4.8' b'3.4' b'1.9' b'0.2' b'Iris-setosa']\n",
      " [b'4.8' b'3.4' b'1.6' b'0.2' b'Iris-setosa']\n",
      " [b'4.8' b'3.1' b'1.6' b'0.2' b'Iris-setosa']\n",
      " [b'4.9' b'2.4' b'3.3' b'1.0' b'Iris-versicolor']\n",
      " [b'4.9' b'2.5' b'4.5' b'1.7' b'Iris-virginica']\n",
      " [b'4.9' b'3.1' b'1.5' b'0.1' b'Iris-setosa']\n",
      " [b'4.9' b'3.1' b'1.5' b'0.1' b'Iris-setosa']]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'\n",
    "iris = np.genfromtxt(url, delimiter = ',', dtype = 'object')\n",
    "names = ('sepallength', 'sepalwidth', 'petallength', 'petalwidth', 'species')\n",
    "\n",
    "print(iris[iris[:, 0].argsort()][:20])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 45、如何在 numpy 数组中找到最常见的值？\n",
    "\n",
    "**难度等级：** L1\n",
    "\n",
    "**问题：** 在鸢尾属植物数据集中找到最常见的花瓣长度值（第 3 列）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'1.5'\n"
     ]
    }
   ],
   "source": [
    "url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'\n",
    "iris = np.genfromtxt(url, delimiter = ',', dtype = 'object')\n",
    "names = ('sepallength', 'sepalwidth', 'petallength', 'petalwidth', 'species')\n",
    "\n",
    "vals, counts = np.unique(iris[:, 2], return_counts = True)\n",
    "print(vals[np.argmax(counts)])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 46、如何找到第一次出现的值大于给定值的位置？\n",
    "\n",
    "**难度等级：** L2\n",
    "\n",
    "**问题：** 在虹膜数据集的 petalwidth 第 4 列中查找第一次出现的值大于 1.0 的位置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([50], dtype=int64)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'\n",
    "iris = np.genfromtxt(url, delimiter = ',', dtype = 'object')\n",
    "\n",
    "np.argwhere(iris[:, 3].astype(float) > 1.0)[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 47、如何将大于给定值的所有值替换为给定的截止值？\n",
    "\n",
    "**难度等级：** L2\n",
    "\n",
    "**问题：** 从数组 a 中，替换所有大于 30 到 30 和小于 10 到 10 的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[27.63 14.64 21.8  30.   10.   10.   30.   30.   10.   29.18 30.   11.25\n",
      " 10.08 10.   11.77 30.   30.   10.   30.   14.43]\n"
     ]
    }
   ],
   "source": [
    "np.set_printoptions(precision = 2)\n",
    "np.random.seed(100)\n",
    "a = np.random.uniform(1, 50, 20)\n",
    "\n",
    "np.clip(a, a_min = 10, a_max = 30)\n",
    "\n",
    "print(np.where(a < 10, 10 ,np.where(a > 30, 30, a)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 48、如何从 numpy 数组中获取最大 n 值的位置？\n",
    "\n",
    "**难度等级：** L2\n",
    "\n",
    "**问题：** 获取给定数组 a 中前 5 个最大值的位置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[27.63 14.64 21.8  42.39  1.23  6.96 33.87 41.47  7.7  29.18 44.67 11.25\n",
      " 10.08  6.31 11.77 48.95 40.77  9.43 41.   14.43]\n",
      "[18  7  3 10 15]\n",
      "[15 10  3  7 18]\n",
      "[41.   41.47 42.39 44.67 48.95]\n",
      "[41.   41.47 42.39 44.67 48.95]\n",
      "[41.   41.47 42.39 44.67 48.95]\n",
      "[48.95 44.67 42.39 41.47 41.  ]\n"
     ]
    }
   ],
   "source": [
    "np.random.seed(100)\n",
    "a = np.random.uniform(1, 50, 20)\n",
    "print(a)\n",
    "\n",
    "# 法一\n",
    "print(a.argsort()[-5:])\n",
    "\n",
    "# 法二\n",
    "print(np.argpartition(-a, 5)[:5])\n",
    "\n",
    "# 以下为取值方法\n",
    "# 法一\n",
    "print(a[a.argsort()][-5:])\n",
    "\n",
    "# 法二\n",
    "print(np.sort(a)[-5:])\n",
    "\n",
    "# 法三\n",
    "print(np.partition(a, kth = -5)[-5:])\n",
    "\n",
    "# 法四\n",
    "print(a[np.argpartition(-a, 5)][:5])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 49、如何计算数组中所有可能值得行数？\n",
    "\n",
    "**难度等级：** L4\n",
    "\n",
    "**问题：** 按行计算唯一值的计数\n",
    "\n",
    "**给定：**\n",
    "``` python\n",
    "np.random.seed(100)\n",
    "arr = np.random.randint(1,11,size=(6, 10))\n",
    "arr\n",
    "array([[ 9,  9,  4,  8,  8,  1,  5,  3,  6,  3],  \n",
    "       [ 3,  3,  2,  1,  9,  5,  1, 10,  7,  3],  \n",
    "       [ 5,  2,  6,  4,  5,  5,  4,  8,  2,  2],  \n",
    "       [ 8,  8,  1,  3, 10, 10,  4,  3,  6,  9],  \n",
    "       [ 2,  1,  8,  7,  3,  1,  9,  3,  6,  2],  \n",
    "       [ 9,  2,  6,  5,  3,  9,  4,  6,  1, 10]])\n",
    "```\n",
    "\n",
    "**期望的输出：**\n",
    "> [[1, 0, 2, 1, 1, 1, 0, 2, 2, 0],  \n",
    "[2, 1, 3, 0, 1, 0, 1, 0, 1, 1],  \n",
    "[0, 3, 0, 2, 3, 1, 0, 1, 0, 0],  \n",
    "[1, 0, 2, 1, 0, 1, 0, 2, 1, 2],  \n",
    "[2, 2, 2, 0, 0, 1, 1, 1, 1, 0],  \n",
    "[1, 1, 1, 1, 1, 2, 0, 0, 2, 1]]\n",
    "\n",
    "输出包含10列，表示从1到10的数字。这些值是各行中数字的计数。\n",
    "例如，cell(0，2)的值为2，这意味着数字3在第一行中恰好出现了2次。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 9  9  4  8  8  1  5  3  6  3]\n",
      " [ 3  3  2  1  9  5  1 10  7  3]\n",
      " [ 5  2  6  4  5  5  4  8  2  2]\n",
      " [ 8  8  1  3 10 10  4  3  6  9]\n",
      " [ 2  1  8  7  3  1  9  3  6  2]\n",
      " [ 9  2  6  5  3  9  4  6  1 10]]\n",
      "[ 1  2  3  4  5  6  7  8  9 10]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[[1, 0, 2, 1, 1, 1, 0, 2, 2, 0],\n",
       " [2, 1, 3, 0, 1, 0, 1, 0, 1, 1],\n",
       " [0, 3, 0, 2, 3, 1, 0, 1, 0, 0],\n",
       " [1, 0, 2, 1, 0, 1, 0, 2, 1, 2],\n",
       " [2, 2, 2, 0, 0, 1, 1, 1, 1, 0],\n",
       " [1, 1, 1, 1, 1, 2, 0, 0, 2, 1]]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.seed(100)\n",
    "arr = np.random.randint(1,11,size=(6, 10))\n",
    "print(arr)\n",
    "\n",
    "def counts_of_all_values_rowwise(arr2d):\n",
    "    num_counts_array = [np.unique(row, return_counts = True) for row in arr2d]\n",
    "    return ([[int(b[a == i]) if i in a else 0 for i in np.unique(arr2d)] for a, b in num_counts_array])\n",
    "\n",
    "print(np.arange(1, 11))\n",
    "counts_of_all_values_rowwise(arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 50、如何将数组转换为平面一维数组？\n",
    "\n",
    "**难度等级：** L2\n",
    "\n",
    "**问题：** 将 array_of_arrays 转换为扁平线性 1d 数组\n",
    "\n",
    "**给定：**\n",
    "``` python\n",
    "arr1 = np.arange(3)\n",
    "arr2 = np.arange(3,7)\n",
    "arr3 = np.arange(7,10)\n",
    "\n",
    "array_of_arrays = np.array([arr1, arr2, arr3])\n",
    "array_of_arrays\n",
    "```\n",
    "\n",
    "**期望的输出：**\n",
    "> array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "array_of_arrays: [array([0, 1, 2]) array([3, 4, 5, 6]) array([7, 8, 9])]\n",
      "[0 1 2 3 4 5 6 7 8 9]\n",
      "[0 1 2 3 4 5 6 7 8 9]\n"
     ]
    }
   ],
   "source": [
    "arr1 = np.arange(3)\n",
    "arr2 = np.arange(3, 7)\n",
    "arr3 = np.arange(7, 10)\n",
    "\n",
    "array_of_arrays = np.array([arr1, arr2, arr3])\n",
    "print('array_of_arrays:', array_of_arrays)\n",
    "\n",
    "# 法一\n",
    "arr_2d = np.array([a for arr in array_of_arrays for a in arr])\n",
    "print(arr_2d)\n",
    "\n",
    "# 法二\n",
    "arr_2d = np.concatenate(array_of_arrays)\n",
    "print(arr_2d)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 51、如何在 numpy 中为数组生成单热编码？\n",
    "\n",
    "**难度等级：** L4\n",
    "\n",
    "**问题：** 计算一次性编码（数组中每个唯一值的虚拟二进制变量）\n",
    "\n",
    "**给定：**\n",
    "``` python\n",
    "np.random.seed(101)\n",
    "arr = np.random.randint(1, 4, size = 6)\n",
    "arr\n",
    "```\n",
    "\n",
    "**期望的输出：**\n",
    "> array([[ 0.,  1.,  0.],  \n",
    "&emsp;&emsp;[ 0.,  0.,  1.],  \n",
    "&emsp;&emsp;[ 0.,  1.,  0.],  \n",
    "&emsp;&emsp;[ 0.,  1.,  0.],  \n",
    "&emsp;&emsp;[ 0.,  1.,  0.],  \n",
    "&emsp;&emsp;[ 1.,  0.,  0.]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 3 2 2 2 1]\n",
      "[[0. 1. 0.]\n",
      " [0. 0. 1.]\n",
      " [0. 1. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 1. 0.]\n",
      " [1. 0. 0.]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 0],\n",
       "       [0, 0, 1],\n",
       "       [0, 1, 0],\n",
       "       [0, 1, 0],\n",
       "       [0, 1, 0],\n",
       "       [1, 0, 0]], dtype=int8)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "np.random.seed(101)\n",
    "arr = np.random.randint(1, 4, size = 6)\n",
    "print(arr)\n",
    "\n",
    "# 法一\n",
    "def one_hot_encodings(arr):\n",
    "    uniqs = np.unique(arr)\n",
    "    out = np.zeros((arr.shape[0], uniqs.shape[0]))\n",
    "    for i, k in enumerate(arr):\n",
    "        out[i, k - 1] = 1\n",
    "    return out\n",
    "\n",
    "print(one_hot_encodings(arr))\n",
    "\n",
    "# 法二\n",
    "(arr[:, None] == np.unique(arr)).view(np.int8)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 52、如何创建按分类变量分组的行号？\n",
    "\n",
    "**难度等级：** L3\n",
    "\n",
    "**问题：** 创建按分类变量分组的行号。使用鸢尾属植物物种的样本作为输入。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa'\n",
      " 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor'\n",
      " 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor'\n",
      " 'Iris-versicolor' 'Iris-virginica' 'Iris-virginica' 'Iris-virginica'\n",
      " 'Iris-virginica' 'Iris-virginica' 'Iris-virginica']\n",
      "[0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 1, 2, 3, 4, 5]\n"
     ]
    }
   ],
   "source": [
    "url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'\n",
    "species = np.genfromtxt(url, delimiter = ',', dtype = 'str', usecols = 4)\n",
    "np.random.seed(100)\n",
    "\n",
    "species_small = np.sort(np.random.choice(species, size = 20))\n",
    "print(species_small)\n",
    "print([i for val in np.unique(species_small) for i, grp in enumerate(species_small[species_small == val])])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 53、如何根据给定的分类变量创建组 ID？\n",
    "\n",
    "**难度等级：** L4\n",
    "\n",
    "**问题：** 根据给定的分类变量创建组 ID。使用鸢尾属植物物种的样本作为输入。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa'\n",
      " 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor'\n",
      " 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor'\n",
      " 'Iris-versicolor' 'Iris-virginica' 'Iris-virginica' 'Iris-virginica'\n",
      " 'Iris-virginica' 'Iris-virginica' 'Iris-virginica']\n",
      "[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2]\n",
      "[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2]\n"
     ]
    }
   ],
   "source": [
    "url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'\n",
    "species = np.genfromtxt(url, delimiter = ',', dtype = 'str', usecols = 4)\n",
    "np.random.seed(100)\n",
    "\n",
    "species_samll = np.sort(np.random.choice(species, size = 20))\n",
    "print(species_samll)\n",
    "\n",
    "# 法一\n",
    "output = [np.argwhere(np.unique(species_small) == s).tolist()[0][0] for val in np.unique(species_small) for s in species_small[species_small == val]]\n",
    "print(output)\n",
    "\n",
    "# 法二\n",
    "output = []\n",
    "uniqs = np.unique(species_small)\n",
    "\n",
    "for val in uniqs:\n",
    "    for s in species_small[species_small == val]:\n",
    "        groupid = np.argwhere(uniqs == s).tolist()[0][0]\n",
    "        output.append(groupid)\n",
    "        \n",
    "print(output)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 54、如何使用numpy 对数组中的项进行排名？\n",
    "\n",
    "**难度等级：** L2\n",
    "\n",
    "**问题：** 为给定的数字数组 a 创建排名\n",
    "\n",
    "**给定：**\n",
    "``` python\n",
    "np.random.seed(10)\n",
    "a = np.random.randint(20, size=10)\n",
    "print(a)\n",
    "# [ 9  4 15  0 17 16 17  8  9  0]\n",
    "```\n",
    "\n",
    "**期望的输出：**\n",
    "> [4 2 6 0 8 7 9 3 5 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Array:  [ 9  4 15  0 17 16 17  8  9  0]\n",
      "[4 2 6 0 8 7 9 3 5 1]\n"
     ]
    }
   ],
   "source": [
    "np.random.seed(10)\n",
    "a = np.random.randint(20, size = 10)\n",
    "print('Array: ', a)\n",
    "\n",
    "print(a.argsort().argsort())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 55、如何使用 numpy 对多维数组中的项进行排名？\n",
    "\n",
    "**难度等级：** L3\n",
    "\n",
    "**问题：** 创建于给定数字数组 a 相同形状的排名数组\n",
    "\n",
    "**给定：**\n",
    "``` python\n",
    "np.random.seed(10)\n",
    "a = np.random.randint(20, size=[2,5])\n",
    "print(a)\n",
    "# [[ 9  4 15  0 17]\n",
    "# [16 17  8  9  0]]\n",
    "```\n",
    "\n",
    "**期望的输出：**\n",
    "> [[4 2 6 0 8]  \n",
    "[7 9 3 5 1]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 9  4 15  0 17]\n",
      " [16 17  8  9  0]]\n",
      "[[3 9 1 7 0]\n",
      " [8 2 5 4 6]]\n"
     ]
    }
   ],
   "source": [
    "np.random.seed(10)\n",
    "a = np.random.randint(20, size = [2,5])\n",
    "print(a)\n",
    "\n",
    "print(a.ravel().argsort().reshape(a.shape))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 56、如何在二维 numpy 数组的每一行中找到最大值？\n",
    "\n",
    "**难度等级：** L2\n",
    "\n",
    "**问题：** 计算给定数组中每行的最大值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[9 9 4]\n",
      " [8 8 1]\n",
      " [5 3 6]\n",
      " [3 3 3]\n",
      " [2 1 9]]\n",
      "[9 8 6 3 9]\n",
      "[9 8 6 3 9]\n"
     ]
    }
   ],
   "source": [
    "np.random.seed(100)\n",
    "a = np.random.randint(1, 10, [5, 3])\n",
    "print(a)\n",
    "\n",
    "# 法一\n",
    "print(np.amax(a, axis = 1))\n",
    "\n",
    "# 法二\n",
    "print(np.apply_along_axis(np.max, arr = a, axis = 1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 57、如何计算二维 numpy 数组每行的最小值？\n",
    "\n",
    "**难度等级：** L3\n",
    "\n",
    "**问题：** 为给定的二维 numpy 数组计算每行的最小值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[9 9 4]\n",
      " [8 8 1]\n",
      " [5 3 6]\n",
      " [3 3 3]\n",
      " [2 1 9]]\n",
      "[4 1 3 3 1]\n",
      "[4 1 3 3 1]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([0.44444444, 0.125     , 0.5       , 1.        , 0.11111111])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.seed(100)\n",
    "a = np.random.randint(1, 10, [5, 3])\n",
    "print(a)\n",
    "\n",
    "# 法一\n",
    "print(np.amin(a, axis = 1))\n",
    "\n",
    "# 法二\n",
    "print(np.apply_along_axis(np.min, arr = a, axis = 1))\n",
    "np.apply_along_axis(lambda x: np.min(x) / np.max(x), arr = a, axis = 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 58、如何在 numpy 数组中找到重复的记录？\n",
    "\n",
    "**难度等级：** L3\n",
    "\n",
    "**问题：** 在给定的 numpy 数组中找到重复的条目（第二次出现以后），并将它们标记为 True。第一次出现应该是 False 的。\n",
    "\n",
    "**给定：**\n",
    "``` python\n",
    "np.random.seed(100)\n",
    "a = np.random.randint(0, 5, 10)\n",
    "print('Array: ', a)\n",
    "```\n",
    "\n",
    "**期望的输出：**\n",
    "> [False True False True False False True True True True]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Array: [0 0 3 0 2 4 2 2 2 2]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([ True,  True,  True,  True])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.seed(100)\n",
    "a = np.random.randint(0, 5, 10)\n",
    "print('Array:', a)\n",
    "\n",
    "out = np.full(a.shape[0], True)\n",
    "\n",
    "unique_positions = np.unique(a, return_index = True)[1]\n",
    "\n",
    "out[unique_positions]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 59、如何找出数字的分组均值？\n",
    "\n",
    "**难度等级：** L3\n",
    "\n",
    "**问题：** 在二维数字数组中查找按分类列分组的数组列的平均值\n",
    "\n",
    "**给定：**\n",
    "``` python\n",
    "url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'\n",
    "iris = np.genfromtxt(url, delimiter=',', dtype='object')\n",
    "names = ('sepallength', 'sepalwidth', 'petallength', 'petalwidth', 'species')\n",
    "```\n",
    "\n",
    "**期望的输出：**\n",
    "> [[b'Iris-setosa', 3.418],  \n",
    "[b'Iris-versicolor', 2.770],  \n",
    "[b'Iris-virginica', 2.974]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[b'Iris-setosa', 3.418],\n",
       " [b'Iris-versicolor', 2.7700000000000005],\n",
       " [b'Iris-virginica', 2.974]]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'\n",
    "iris = np.genfromtxt(url, delimiter = ',', dtype = 'object')\n",
    "names = ('sepallength', 'sepalwidth', 'petallength', 'petalwidth', 'species')\n",
    "\n",
    "numeric_column = iris[:, 1].astype('float')\n",
    "grouping_column = iris[:, 4]\n",
    "\n",
    "[[group_val, numeric_column[grouping_column == group_val].mean()] for group_val in np.unique(grouping_column)]\n",
    "\n",
    "output = []\n",
    "for group_val in np.unique(grouping_column):\n",
    "    output.append([group_val, numeric_column[grouping_column == group_val].mean()])\n",
    "    \n",
    "output"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 60、如何将 PIL 图像转换为 numpy 数组？\n",
    "\n",
    "**难度等级：** L3\n",
    "\n",
    "**问题：** 从以下 URL 导入图像并将其转换为 numpy 数组\n",
    "\n",
    "**给定：**\n",
    "``` python\n",
    "URL = 'https://upload.wikimedia.org/wikipedia/commons/8/8b/Denali_Mt_McKinley.jpg'\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[  9  72 125]\n",
      "  [ 10  73 126]\n",
      "  [ 11  74 127]\n",
      "  ...\n",
      "  [ 44 104 154]\n",
      "  [ 44 104 154]\n",
      "  [ 42 103 147]]\n",
      "\n",
      " [[ 10  73 126]\n",
      "  [ 11  74 127]\n",
      "  [ 12  75 128]\n",
      "  ...\n",
      "  [ 46 106 156]\n",
      "  [ 46 106 156]\n",
      "  [ 48 109 154]]\n",
      "\n",
      " [[ 11  74 127]\n",
      "  [ 12  75 128]\n",
      "  [ 13  76 129]\n",
      "  ...\n",
      "  [ 47 107 157]\n",
      "  [ 47 107 157]\n",
      "  [ 45 105 155]]\n",
      "\n",
      " ...\n",
      "\n",
      " [[ 15  28  36]\n",
      "  [ 24  46  60]\n",
      "  [ 16  33  43]\n",
      "  ...\n",
      "  [ 23  60  79]\n",
      "  [ 39  73  98]\n",
      "  [ 42  79 106]]\n",
      "\n",
      " [[ 32  50  60]\n",
      "  [ 21  48  65]\n",
      "  [ 27  49  62]\n",
      "  ...\n",
      "  [ 23  46  54]\n",
      "  [ 22  41  56]\n",
      "  [ 20  43  59]]\n",
      "\n",
      " [[ 29  55  68]\n",
      "  [ 26  59  78]\n",
      "  [ 19  48  64]\n",
      "  ...\n",
      "  [ 25  71  95]\n",
      "  [ 34  75 107]\n",
      "  [ 27  64  91]]]\n"
     ]
    }
   ],
   "source": [
    "from io import BytesIO\n",
    "from PIL import Image\n",
    "import PIL, requests\n",
    "\n",
    "URL =  'https://upload.wikimedia.org/wikipedia/commons/8/8b/Denali_Mt_McKinley.jpg'\n",
    "response = requests.get(URL)\n",
    "\n",
    "I = Image.open(BytesIO(response.content))\n",
    "I = I.resize([150, 150])\n",
    "\n",
    "arr = np.asarray(I)\n",
    "print(arr)\n",
    "\n",
    "im = PIL.Image.fromarray(np.uint8(arr))\n",
    "Image.Image.show(im)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 61、如何删除 numpy 数组中所有缺少的值？\n",
    "\n",
    "**难度等级：** L2\n",
    "\n",
    "**问题：** 从一堆 numpy 数组中删除所有 NaN 值\n",
    "\n",
    "**给定：**\n",
    "> np.array([1, 2, 3, np.nan, 5, 6, 7, np.nan])\n",
    "\n",
    "**期望的输出：**\n",
    "> array([1., 2., 3., 5., 6., 7.])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 2., 3., 5., 6., 7.])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.array([1, 2, 3, np.nan, 5, 6, 7, np.nan])\n",
    "\n",
    "a[~np.isnan(a)]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 62、如何计算两个数组之间的欧氏距离？\n",
    "\n",
    "**难度等级：** L3\n",
    "\n",
    "**问题：** 计算两个数组 a 和数组 b 之间的欧氏距离\n",
    "\n",
    "**给定：**\n",
    "``` python\n",
    "a = np.array([1, 2, 3, 4, 5])\n",
    "b = np.array([4, 5, 6, 7, 8])\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6.708203932499369\n"
     ]
    }
   ],
   "source": [
    "a = np.array([1, 2, 3, 4, 5])\n",
    "b = np.array([4, 5, 6, 7, 8])\n",
    "\n",
    "dist = np.linalg.norm(a - b)\n",
    "print(dist)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 63、如何在一维数组中找到所有的局部极大值（或峰值）？\n",
    "\n",
    "**难度等级：** L4\n",
    "\n",
    "**问题：**找到一个一维数字组 a 中的所有峰值，峰顶是两边被较小数值包围的点\n",
    "\n",
    "**给定：**\n",
    "> a = np.array([1, 3, 7, 1, 2, 6, 0, 1])\n",
    "\n",
    "**期望的输出：**\n",
    "> array([2, 5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 5], dtype=int64)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([1, 3, 7, 1, 2, 6, 0, 1])\n",
    "doublediff = np.diff(np.sign(np.diff(a)))\n",
    "peak_locations = np.where(doublediff == -2)[0] + 1\n",
    "peak_locations"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 64、如何从二维数组中减去一维数组，其中一维数组的每一项从各自的行中减去？\n",
    "\n",
    "**难度等级：** L2\n",
    "\n",
    "**问题：** 从 2d 数组 a_2d 中减去一维数组 b_1d 的每一项从 a_2d 的相应行中减去\n",
    "\n",
    "**给定：**\n",
    "``` python\n",
    "a_2d = np.array([[3,3,3],[4,4,4],[5,5,5]])\n",
    "b_1d = np.array([1,1,1]\n",
    "```\n",
    "\n",
    "**期望的输出：**\n",
    "> [[2 2 2]  \n",
    "[2 2 2]  \n",
    "[2 2 2]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2 2 2]\n",
      " [2 2 2]\n",
      " [2 2 2]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "a_2d = np.array([[3, 3, 3], [4, 4, 4], [5, 5, 5]])\n",
    "b_1d = np.array([1, 2, 3])\n",
    "                \n",
    "print(a_2d - b_1d[:, None])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 65、如何查找数组中项的第 n 次重复索引？\n",
    "\n",
    "**难度等级：** L2\n",
    "\n",
    "**问题：** 找出 x 中数字 1 的第 5 次重复的索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "x = np.array([1, 2, 1, 1, 3, 4, 3, 1, 1, 2, 1, 1, 2])\n",
    "n = 5\n",
    "\n",
    "# 法一\n",
    "print([i for i, v in enumerate(x) if v == 1][n - 1])\n",
    "\n",
    "# 法二\n",
    "print(np.where(x == 1)[0][n - 1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 66、如何将 numpy 的 datetime 64对象转换为 datetime 的 datetime 对象？\n",
    "\n",
    "**难度等级：** L2\n",
    "\n",
    "**问题：** 将 numpy 的 datetime64 对象转换为 datetime 的 datetime 对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2018-02-25 22:10:10\n",
      "2018-02-25 22:10:10\n"
     ]
    }
   ],
   "source": [
    "dt64 = np.datetime64('2018-02-25 22:10:10')\n",
    "\n",
    "# 法一\n",
    "from datetime import datetime\n",
    "print(dt64.tolist())\n",
    "\n",
    "#法二\n",
    "print(dt64.astype(datetime))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 67、如何计算 numpy 数组的移动平均值？\n",
    "\n",
    "**难度等级：** L3\n",
    "\n",
    "**问题：** 对于给定的一维数组，计算窗口大小为 3 的移动平均值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "array: [8 8 3 7 7 0 4 2 5 2]\n",
      "[6.33 6.   5.67 4.67 3.67 2.   3.67 3.  ]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([6.33333333, 6.        , 5.66666667, 4.66666667, 3.66666667,\n",
       "       2.        , 3.66666667, 3.        ])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def moving_average(a, n = 3):\n",
    "    ret = np.cumsum(a, dtype = float)\n",
    "    ret[n:] = ret[n:] - ret[:-n]\n",
    "    return ret[n - 1:] / n\n",
    "\n",
    "np.random.seed(100)\n",
    "Z = np.random.randint(10, size = 10)\n",
    "print('array:', Z)\n",
    "\n",
    "# 法一\n",
    "print(moving_average(Z, n = 3).round(2))\n",
    "\n",
    "# 法二\n",
    "np.convolve(Z, np.ones(3) / 3, mode = 'valid')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 68、如何在给定起始点、长度和步骤的情况下创建一个 numpy 数组序列？\n",
    "\n",
    "**难度等级：** L2\n",
    "\n",
    "**问题：** 创建长度为 10 的 numpy 数组，从 5 开始，在连续的数字之间的步长为 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 5,  8, 11, 14, 17, 20, 23, 26, 29, 32])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "length = 10\n",
    "start = 5\n",
    "step = 3\n",
    "\n",
    "def seq(start, length, step):\n",
    "    end = start + (step * length)\n",
    "    return np.arange(start, end, step)\n",
    "\n",
    "seq(start, length, step)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 69、如何填写不规则系列的 numpy 日期中的缺失日期？\n",
    "\n",
    "**难度等级：** L3\n",
    "\n",
    "**问题：** 给定一系列不连续的日期序列。填写缺失的日期，使其成为连续的日期序列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['2018-02-01' '2018-02-03' '2018-02-05' '2018-02-07' '2018-02-09'\n",
      " '2018-02-11' '2018-02-13' '2018-02-15' '2018-02-17' '2018-02-19'\n",
      " '2018-02-21' '2018-02-23']\n",
      "['2018-02-01' '2018-02-02' '2018-02-03' '2018-02-04' '2018-02-05'\n",
      " '2018-02-06' '2018-02-07' '2018-02-08' '2018-02-09' '2018-02-10'\n",
      " '2018-02-11' '2018-02-12' '2018-02-13' '2018-02-14' '2018-02-15'\n",
      " '2018-02-16' '2018-02-17' '2018-02-18' '2018-02-19' '2018-02-20'\n",
      " '2018-02-21' '2018-02-22' '2018-02-23']\n",
      "['2018-02-01' '2018-02-02' '2018-02-03' '2018-02-04' '2018-02-05'\n",
      " '2018-02-06' '2018-02-07' '2018-02-08' '2018-02-09' '2018-02-10'\n",
      " '2018-02-11' '2018-02-12' '2018-02-13' '2018-02-14' '2018-02-15'\n",
      " '2018-02-16' '2018-02-17' '2018-02-18' '2018-02-19' '2018-02-20'\n",
      " '2018-02-21' '2018-02-22' '2018-02-23']\n"
     ]
    }
   ],
   "source": [
    "dates = np.arange(np.datetime64('2018-02-01'), np.datetime64('2018-02-25'), 2)\n",
    "print(dates)\n",
    "\n",
    "filled_in = np.array([np.arange(date, (date + d)) for date, d in zip(dates, np.diff(dates))]).reshape(-1)\n",
    "\n",
    "output = np.hstack([filled_in, dates[-1]])\n",
    "print(output)\n",
    "\n",
    "out = []\n",
    "for date, d in zip(dates, np.diff(dates)):\n",
    "    out.append(np.arange(date, (date + d)))\n",
    "               \n",
    "filled_in = np.array(out).reshape(-1)\n",
    "               \n",
    "output = np.hstack([filled_in, dates[-1]])\n",
    "print(output)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 70、如何从给定的一维数组创建步长？\n",
    "\n",
    "**难度等级：** L4\n",
    "\n",
    "**问题：** 从给定的一维数组 arr 中，利用步进生成一个二维矩阵，窗口长度为 4，步距为 2，类似于 [[0, 1, 2, 3,], [2, 3, 4, 5], [4, 5, 6, 7]..]\n",
    "\n",
    "**给定：**\n",
    "``` python\n",
    "arr = np.arange(15)\n",
    "arr\n",
    "> array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14])\n",
    "```\n",
    "\n",
    "**期望的输出：**\n",
    "> [[ 0  1  2  3]  \n",
    "[ 2  3  4  5]  \n",
    "[ 4  5  6  7]  \n",
    "[ 6  7  8  9]  \n",
    "[ 8  9 10 11]  \n",
    "[10 11 12 13]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 2  3  4  5]\n",
      " [ 4  5  6  7]\n",
      " [ 6  7  8  9]\n",
      " [ 8  9 10 11]\n",
      " [10 11 12 13]]\n"
     ]
    }
   ],
   "source": [
    "def gen_strides(a, stride_len = 5, window_len = 5):\n",
    "    n_strides = ((a.size - window_len) // stride_len) + 1\n",
    "    return np.array([a[s:(s + window_len)] for s in np.arange(0, n_strides * stride_len, stride_len)])\n",
    "\n",
    "print(gen_strides(np.arange(15), stride_len = 2, window_len = 4))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
